how to create a generic singleton class in Unity?

拈花ヽ惹草 提交于 2019-12-12 04:44:30

问题


I'm a beginner of Unity.

I had a question while studying.

I refer to the document below.

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;
    }
}

if instance was null, Why GameObject to add to AddComponent ?

and Why use that FindObject function ?

and Why use Singleton in Unity?

I don't Know Singleton Overall flow..

please give to me Code Review..

As a beginner, I do not know much. I need your help.

Please give me your think.


回答1:


if instance was null, Why GameObject to add to AddComponent ?

If the instance of the script you want to create is null if (_instance == null):

1.Create new GameObject

GameObject singleton = new GameObject();

2.Create new instance of that script and attach it to the GameObject that is created above. In Unity, a component must be attached to a GameObject. The AddComponent function is used to attach a component to a GameObject.

_instance = singleton.AddComponent<T>();

Why use that FindObject function ?

If the instance of the script you want to create is null if (_instance == null), check if that script instance already exist in the scene. The FindObjectOfType function is used to find this type of script only. Let's say that we have a script called SceneLoader and we pass SceneLoader to the Singleton class, it will check if an instance of SceneLoader is already in the scene and return that instance of it. It will return null if it does not exist.

Why use Singleton in Unity?

You use it when you only want to have one instance of a type of script in the scene. Also, with DontDestroyOnLoad, it means that this instance will still be there even when next scene is loaded. It will not be destroyed like other scripts.

please give to me Code Review

You can ask for code improvement on the codereview site. If you are new to Unity you can find Unity project tutorials on their site that can get you started easily here.



来源:https://stackoverflow.com/questions/46438184/how-to-create-a-generic-singleton-class-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!