4 ms·
I find the best way to learn is by just building. You quickly run into questions, which can then be resolved one by one. But having said that, here's some tips.
by animal531 6y ago
I find the best way to learn is by just building. You quickly run into questions, which can then be resolved one by one. But having said that, here's some tips. There's no single "right" way, but depending on your game idea you'll run into your own challenges and issues.
Learn what GameObject's and Components are and how they work. In Unity you have MonoBehaviour derived classes, but then you might also want some of your own non-Mono classes (for example an Entity-Character-Player/Civilian/Enemy inheritance structure).
For my code framework in Unity I create a GameObject in my scene that I call MainScripts. On this I'm going to attach all my generic Mono classes such as Game, MapList, AudioManager, UnitList etc. I call DontDestroyOnLoad on MainScripts so that it will last through all successive scenes.
Game is the main game class, I use its update to drive as many updates as I can (for performance reasons you want as few Unity Update methods running as possible). So for example inside of game's update I'm also calling my input's update, my own timers etc.
The next big problem is how does code in random class X access another class? For Mono classes you can go through the whole GameObject.Find/GetComponent in your Awake or Start methods and store them; BUT you might not have direct references to all your own code that doesn't inherit from MonoBehaviour.
For that I create a Static class. I'm going to place static references in here for everything global in my game, the previously mentioned MainScripts, Game, etc. As a random example, some Mono class might need to know if the control key is down. Since we're doing all input in our own input class we don't want to test through Unity again if control is down. Our input will already have a flag set, so then we just test something like if (Static.input.isAnyControlDown).
To setup Static we call our custom Static.Create from Mono Game.Start, which Unity will automatically call. Place Game very high (low #) in the Unity Script Execution Order so it will always be created first.
After that you have a code framework to hang everything off of. Hope it makes sense!
- splatcollision 6y agoI am literally replying to this so I can find it later. Thanks for the architectural advice!
- Jach 6y agoLittle HN feature: if you click a comment's timestamp, you'll be able to "favorite" it. You can look at your (or anyone else's) favorites from the profile page.
- zemo 6y ago... all this and no mention of Scriptable Objects? Not sure about this
- matsemann 6y agoI think your first sentence is correct. And that maybe you should have stopped there. Spending ages trying to do things "the right way" instead of just hacking it together and actually learning is what makes it hard to get going. Basically making your own framework on top of Unity (as per your tips) is not the best way to start.