【问题标题】:how to create a generic singleton class in Unity?如何在 Unity 中创建一个通用的单例类?
【发布时间】:2017-09-27 18:31:04
【问题描述】:

我是 Unity 的初学者。

我在学习时遇到了一个问题。

我参考下面的文件。

using UnityEngine;

public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T _instance;

    private static object _lock = new object();

    public static T Instance
    {
        get
        {
            if (applicationIsQuitting) {
                Debug.LogWarning("[Singleton] Instance '"+ typeof(T) +
                    "' already destroyed on application quit." +
                    " Won't create again - returning null.");
                return null;
            }

            lock(_lock)
            {
                if (_instance == null)
                {
                    _instance = (T) FindObjectOfType(typeof(T));

                    if ( FindObjectsOfType(typeof(T)).Length > 1 )
                    {
                        Debug.LogError("[Singleton] Something went really wrong " +
                            " - there should never be more than 1 singleton!" +
                            " Reopening the scene might fix it.");
                        return _instance;
                    }

                    if (_instance == null)
                    {
                        GameObject singleton = new GameObject();
                        _instance = singleton.AddComponent<T>();
                        singleton.name = "(singleton) "+ typeof(T).ToString();

                        DontDestroyOnLoad(singleton);

                        Debug.Log("[Singleton] An instance of " + typeof(T) + 
                            " is needed in the scene, so '" + singleton +
                            "' was created with DontDestroyOnLoad.");
                    } else {
                        Debug.Log("[Singleton] Using instance already created: " +
                            _instance.gameObject.name);
                    }
                }

                return _instance;
            }
        }
    }

    private static bool applicationIsQuitting = false;

    public void OnDestroy () {
        applicationIsQuitting = true;
    }
}

如果实例为空,为什么要将 GameObject 添加到 AddComponent 中?

为什么要使用 FindObject 函数?

为什么在 Unity 中使用 Singleton?

我不知道单例整体流程..

请给我代码审查..

作为初学者,我知道的不多。我需要你的帮助。

请给我你的想法。

【问题讨论】:

    标签: c# generics unity3d singleton


    【解决方案1】:

    如果实例为空,为什么要将 GameObject 添加到 AddComponent 中?

    如果你要创建的脚本实例为空if (_instance == null):

    1.创建新游戏对象

    GameObject singleton = new GameObject();
    

    2。创建该脚本的新实例并将其附加到上面创建的 GameObject。在 Unity 中,组件必须附加到 GameObject。 AddComponent 函数用于将组件附加到游戏对象。

    _instance = singleton.AddComponent<T>();
    

    为什么要使用 FindObject 函数?

    如果您要创建的脚本实例为空if (_instance == null),请检查该脚本实例是否已存在于场景中。 FindObjectOfType 函数仅用于查找此类脚本。假设我们有一个名为SceneLoader 的脚本,我们将SceneLoader 传递给Singleton 类,它将检查SceneLoader 的实例是否已经在场景中并返回它的实例。如果不存在,它将返回null

    为什么在 Unity 中使用 Singleton?

    当您只想在场景中拥有一个一种脚本类型的实例时,您可以使用它。此外,使用DontDestroyOnLoad,这意味着即使加载了下一个场景,该实例仍将存在。它不会像其他脚本一样被销毁。

    请给我代码审查

    您可以在codereview 网站上请求改进代码。如果您是 Unity 新手,您可以在他们的网站上找到 Unity 项目教程,这些教程可以帮助您轻松入门 here

    【讨论】:

    • 你能和我一对一聊天吗?
    • 我很少聊天,但会创建一个。我可以停留几分钟。
    • 你的聊天室标题是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2023-02-16
    • 1970-01-01
    相关资源
    最近更新 更多