Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Who Manages the Managers? Unity system management

Who Manages the Managers? Unity system management

Whistling Alpaca Studios
Whistling Alpaca Studios
Stouthollow Tales: Devlog · · 2 min read
5,544 0

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

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: 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:


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".


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:


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.


Keep chuckling, Mr Blacksmith...Keep chuckling.

Recommended resources

Game Engines

See full guide
Unreal Engine 5 Best Practices cover
Editor pick

Unreal Engine 5 Best Practices

Amazon · Book

Written by multi-award-winning Unreal generalist Tyson J. Butler-Boschma, Founder and Creative Director of Toybox Games Studios, this book addresses common challenges you face when advancing your expertise in lighting, environment design, and cinematic storytelling.

GameDev.net may earn a commission if you purchase through these links. This helps fund the site at no extra cost to you.

GameDev.net may earn a commission if you purchase through these links. This helps fund the site at no extra cost to you.

Discussion

Loading comments...