Unity 单例管理器类 [英] Unity singleton manager classes

查看:21
本文介绍了Unity 单例管理器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Unity 中,有什么好方法可以创建一个可以作为具有静态变量的全局类随处访问的单例游戏管理器,这些静态变量将向每个提取这些值的类输出相同的常量值?在 Unity 中实现它的方法是什么?我必须将它附加到游戏对象吗?它可以只存在于文件夹中而不在视觉中出现在场景中吗?

In Unity, whats a good way to create a singleton game manager that can be accessed everywhere as a global class with static variables that will spit the same constant values to every class that pulls those values? And what would be the way to implement it in Unity? Do I have to attach it to a GameObject? Can it just be there in a folder without being in the scene visually?

推荐答案

一如既往:视情况而定.我使用两种类型的单例,附加到 GameObject 的组件和不是从 MonoBehaviour 派生的独立类.IMO 的总体问题是实例如何绑定到场景、游戏对象的生命周期……而且不要忘记有时拥有一个组件更方便,尤其是引用其他 MonoBehaviour 对象更容易、更安全.

Like always: it depends. I use singletons of both kinds, components attached to GameObject and standalone classes not derived from MonoBehaviour. IMO the overall question is how are instances bound to the lifcycle of scenes, game objects, ... And not to forget sometimes it is more convenient to have a component especially referencing other MonoBehaviour objects is easier and safer.

  1. 有些类只需要提供一些值,例如需要在调用时从持久层加载设置的配置类.我将这些类设计为简单的单例.
  2. 另一方面,一些对象需要知道场景何时开始,即 Start 被调用或必须在 Update 或其他方法中执行操作.然后我将它们实现为组件,并将它们附加到一个在加载新场景后仍然存在的游戏对象.
  1. There are classes that just need to provide some values like for example a config class that needs to load settings from persistence layer when called. I design theese classes as simple singletons.
  2. On the other hand some objects need to know when a scene is started i.e. Start is called or have to perform actions in Update or other methods. Then I implement them as component and attach them to a game object that survives loading new scenes.

我设计了基于组件的单例(类型 2),包含两部分:一个名为 Main 的持久 GameObject,它保存所有组件和一个名为 MainComponentManager 用于管理它.一些演示代码:

I designed component based singletons (type 2) with two parts: a persistent GameObject called Main, which holds all components and a flat singleton (type 1) called MainComponentManager for managing it. Some demo code:

public class MainComponentManger {
    private static MainComponentManger instance;
    public static void CreateInstance () {
        if (instance == null) {
            instance = new MainComponentManger ();
            GameObject go = GameObject.Find ("Main");
            if (go == null) {
                go = new GameObject ("Main");
                instance.main = go;
                // important: make game object persistent:
                Object.DontDestroyOnLoad (go);
            }
            // trigger instantiation of other singletons
            Component c = MenuManager.SharedInstance;
            // ...
        }
    }

    GameObject main;

    public static MainComponentManger SharedInstance {
        get {
            if (instance == null) {
                CreateInstance ();
            }
            return instance;
        }
    }

    public static T AddMainComponent <T> () where T : UnityEngine.Component {
        T t = SharedInstance.main.GetComponent<T> ();
        if (t != null) {
            return t;
        }
        return SharedInstance.main.AddComponent <T> ();
    }

现在其他想要注册为 Main 组件的单例看起来像:

Now other singletons that want to register as Main component just look like:

public class AudioManager : MonoBehaviour {
    private static AudioManager instance = null;
    public static AudioManager SharedInstance {
        get {
            if (instance == null) {
                instance = MainComponentManger.AddMainComponent<AudioManager> ();
            }
            return instance;
        }
    }

这篇关于Unity 单例管理器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆