- Home /
Best practices for Unity GameManager object?
I'm working on some solutions and I'm attempting to have a GameManager class for all of my games. This should:
Persist across levels/scenes.
Be serializable and visible in the Unity Editor.
Be scene-agnostic - meaning, I can start the game at any scene for debugging purposes.
I've been able to accomplish #1 with DontDestroyOnLoad
. However, I'm having conflicts between #2 and #3.
If I want #2 - "the object should be serializable and visible in the Unity Editor" - I have to derive from MonoBehavior, or ScriptableObject. The issue with Monobehavior is that it requires a gameObject, meaning that there must be an instance in a scene somewhere. ScriptableObjects, unfortunately, are similar; while they don't have to be attached to a gameobject, they must be referenced from an active object (as a property) in order to be used.
If I want #3 - starting the game from any scene - I can make the class a singleton, or a static class; this means I can instantiate the object in code without having to have a gameObject in the scene, but then I run into configuration issues. Creating the class from code means that Unity has no way to serialize data in the editor (unless I'm missing something fundamental).
I attempted to use AssetDatabase.LoadAssetAtPath
, thinking that I could generate my Singleton by loading my GameManager scriptableObject from disk, but AssetDatabase
is in the UnityEditor namespace. It won't work in a build.
public class _GameManager : ScriptableObject {
private static _GameManager _instance;
public static _GameManager getInstance() {
if (_instance == null) {
Debug.Log ("GAMEMANAGER INSTANCE NULL");
// _instance = ScriptableObject.CreateInstance<_GameManager>();
_instance = AssetDatabase.LoadAssetAtPath("path/to/gamemanager");
}
return _instance;
}
What's best practice for this? Do I need to create a GameManager prefab and instantiate that when the game runs? That would allow me to satisfy #2, and maybe #3, but it seems....incorrect, somehow, since a prefab is supposed to be a template.
Given the needs you're describing, you might try the prefab solution and see how it goes. You can make that prefab a "resource" and load it with Resources.Load, the first time it's needed.
One handy thing with $$anonymous$$onoBehaviour is that you'll get Update calls, if those are useful for your manager.
I agree it's not a perfect solution, but it seems to be the best I've seen.
Answer by brunoleos · Dec 02, 2016 at 06:30 PM
Now with multi-scene editing being better supported, I'm using a main scene that is always loaded and have the persistent managers, and the other scenes are loaded addictively.
Your answer
Follow this Question
Related Questions
Best pracices: Object communication without references 2 Answers
Best Practices: Compiling for iOS 0 Answers
How to Make Things Happen the Right Way 1 Answer
Best Practice: 3D GUI or Built-in uGUI? 2 Answers
Best practices for editing large levels 0 Answers