Who Manages the Managers? Unity system management

Published September 25, 2019
Advertisement

At this point in development you could say that we have a few managers running the various elements of the game:

image.thumb.png.b9e3b7cf9e05467c486bd1c3c576817a.png

Anything that requires singleton behavior gets a manager. From the guilds to the player's stores to the zones an inventory icons we're leaning heavily on these managers to get the job done. Anything that doesn't need to be a singleton and can exist as a static class just gets to set there being static.

To expand on why you may need managers which are singletons, the way we handle inventory icons is a good example. We have a ton of icons, such as these: Sprite-Test_Icons.png.d5de2e451975c86678d88fe5554c31b8.png that we need to feed into the game and map off to an item type (they are default icons for item types). With our singleton manager we can simply pass these sliced icons as parameters in our prefab's inspector:

image.png.e997eec02ae8136a9f172562709e1b5b.png

And now there's a reference to our items mapped to their type and everything is nice and clean. Except...How do you access the ItemIconManager prefab? Are you going to type GameObject.find(...)? Absolutely not, that's where the Manager's Manager comes in! Manager's Manager physically hurt my heart to write, so I ended up naming it the "Manager Loader".

image.png.c37ebf3a1b88e5c92936ad71375d69d3.png

Now your ManagerLoader can maintain a reference to all of your prefab managers. And load them individually. The only person in the hierarchy here that needs to be marked DontDestroyOnLoad is the ManagerLoader itself, so you don't repeat yourself in all those child managers.

My Manager Loader has 2 things that are important here. A list of references to the Prefabs of the Managers, and a static List of the instantiated prefabs:

image.png.d0cc684c55d959f883cda0ec7b9577e0.png

Then all I do is instantiate the managers and load them into my static list like so:


    private void LoadAllManagers()
    {
        if(Managers != null && Managers.Count > 0)
        {
            InstManagers = new List<GameObject>();
            foreach (GameObject go in Managers)
            {
                GameObject instanceGo = Instantiate(go);
                InstManagers.Add(instanceGo);

                instanceGo.name = go.name;
                instanceGo.transform.SetParent(transform.root);
            }
        }
    }

At this point we have static references to all our Managers inside of our Manager Manager...Er, Manager Loader, and if we want to reference them its as easy as:


    public static GameObject GetManagerByName(string name)
    {
        foreach(GameObject gameObject in InstManagers)
        {
            if (gameObject.name.ToLower().Equals(name.ToLower()))
            {
                return gameObject;
            }
        }
        return null;
    }

Now instead of searching through every game object we only have to go through as many managers as you have. As long as you're comfortable sacrificing space for speed, you're set up for success here. I guess you could even store the managers in a dictionary for even faster reference! Now there's an idea.

blacksmith_laugh.gif.45f7ff8096767859260d8c85b2045a22.gif

Keep chuckling, Mr Blacksmith...Keep chuckling.

0 likes 0 comments

Comments

Stragen

I've used something similar... except instead of going with the list approach, i made a dictionary that referred to them by name, saving me writing out a search block like you have above.

Still does a similar search, still sacrifices memory space for convenience, just a couple of fewer lines of code.

September 25, 2019 04:23 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement