How to use the Setup Trick in Unity
Lars Wobus
Senior Animation Programmer @3D Interaction Technologies // I bring your 3D models to life on the web, desktop and mobile devices
#gamedevelopment?,?#unity3d?,?#csharp?,?#3d?,?#programming?,?#gaming
Foreword
Sometimes I am watching videos of Nick Chapsas, a Youtube content creator for C# and .Net. The topic of one of his videos is called The setup trick that .Net libraries use.
I recently applied this idea to one of my Typescript projects, where I needed to prepare some classes that will be used by other developers in the future. However, since my newsletter is currently focused on Unity, I will introduce you to the idea using MonoBehaviours.
Design
In Figure 1 you can see a class which contains typical attributes of different character classes of a roleplaying game. For the sake of simplicity, the fields are all marked as public. The most important part here is the options parameter inside the constructor!
As you can see, this parameter is not used in the Farmer script. If you now add the Farmer script to any GameObject in your scene, you will see in the Unity Inspector that all fields contain their default values.
However, if you add the Warrior or Mage script (see Figures 2 and 3) to any GameObject instead, you will notice that some attributes have different values.
领英推荐
Final Words
I have only described one use case within Unity projects today. But there are several others. So I highly recommend to watch the Youtube video for inspiration.
The setup trick is usually worthwhile when there are a large number of configuration options and implementing many method overloads does not seem reasonable.
That's it for today.
What do you think about?the Setup Trick? Have you used it before or do you have concerns about its usefulness? Or do you think that I have forgotten an important aspect, then feel free to leave a comment.
And if you are interested in topics like?#gamedesign, project organization or software design patterns, don't forget to press the?Follow?button on?my profile page?or add me as a contact. Not convinced yet? Well, maybe you will be once you read some of my?other articles.