"Writing Infrastructure for Unity"

Posted by Ryan C. Scott on Tue 13 April 2010
Lately I've been dealing with writing lots of infrastructure for my project in Unity and have been sorely missing some of the things one would take for granted in a custom engine. Specifically in the order of initialization of scripts.
In Unity's model, which I generally really enjoy working with, you need to roll your own system for ensuring that specific scripts get initialized before others (I posted mine on the Unify wiki at http://www.unifycommunity.com/wiki/index.php?title=Startup_Manager).

(EDIT: If you only need the order managed at scene startup (and not per object) a simpler and more project-agnostic approach is to have a manager that on its Start() sends out "Messages", in the Unity sense by way of SendMessage(), for a series of "Run levels", such as RunLevel_0, RunLevel_1, etc. Objects needing to participate in that system can do their initializations in the appropriate run level by implementing the corresponding function)

Again, in most cases this isn't much of an issue, but take for example the usage case of wanting or needing a handful of objects or managers in every scene you create (or perhaps more specifically the ones functioning as levels in the traditional sense). Currently you have to juggle these in like you would anything else.

Now it's not a horrible task to just make sure that each scene has the appropriate prefabs in it, but this is a situation where you would not be tasked with such a thing at all if you could tap into the deeper layers like you would with a custom engine.

What I would really like to see would be a classification of script or hooks in a manager somewhere in Unity itself that would handle instantiating those scripts for you. All of your other scripts could then safely assume that those objects have been instantiated and access them using a singleton pattern. Writing infrastructure like quadtrees and the like to manage your scene objects would then be much simpler and this custom layer you're putting on top of Unity that essentially comprises the platform which you are building your game(s) on could be further formalized and made more robust.