如何在Unity中创建一个通用的单例类? [英] how to create a generic singleton class in Unity?
问题描述
我在学习的时候有一个问题。
以下文件。
使用UnityEngine;
:
公共类Singleton< T> :MonoBehaviour其中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)+
'已在应用程序退出时被销毁。+
不会再创建 - 返回null。
返回null;
$(b
$ (T));
if(FindObjectsOfType(typeof(T))。Length> 1)
{
Debug.LogError([Singleton]出错了+
- 永远不要超过1个单身!+
重新打开场景可能会修复它。);
return _instance;
}
if(_instance == null)
{
GameObject singleton = new GameObject();
_instance = singleton.AddComponent< T>();
singleton.name =(singleton)+ typeof(T).ToString();
DontDestroyOnLoad(singleton);
Debug.Log([Singleton]在场景中需要一个+ typeof(T)+
的实例,所以''+ singleton +
''是用DontDestroyOnLoad创建。);
} else {
Debug.Log([Singleton]使用已创建的实例:+
_instance.gameObject.name);
}
}
return _instance;
}
}
}
private static bool applicationIsQuitting = false;
public void OnDestroy(){
applicationIsQuitting = true;
$ b如果实例为null,为什么要将GameObject添加到AddComponent ?
为什么使用FindObject函数?
为什么在Unity中使用Singleton?
我不知道Singleton整体流程。
>
请给我代码审查。
作为初学者,我不太了解。我需要你的帮助。
请给我你的想法。
解决方案
如果实例为null,为什么要将GameObject添加到AddComponent?
如果脚本实例如果(_instance == null)
1 ,创建新的游戏对象
GameObject singleton = new GameObject();
2 。创建该脚本的新实例并将其附加到GameObject这是上面创建的。在Unity中,组件必须附加到GameObject。
AddComponent
函数用于将组件附加到GameObject。_instance = singleton.AddComponent< T>();
为什么使用FindObject函数?
如果您要创建的脚本实例为空
if(_instance == null)
,请检查if该脚本实例已经存在于场景中。FindObjectOfType
函数仅用于查找此类型的脚本。假设我们有一个名为SceneLoader
的脚本,并且将SceneLoader
传递给Singleton
class,它将检查
SceneLoader
的实例是否已经在场景中并返回该实例。如果它不存在,它将返回null
。
为什么在Unity中使用Singleton ?b / b>
当您只想在场景中使用一种脚本类型的实例时。另外,使用
DontDestroyOnLoad
时,这意味着即使加载下一个场景,该实例仍然存在。它不会像其他脚本一样销毁。
请给我代码审查
您可以在 codereview 网站上寻求代码改进。如果您是Unity新手,可以在其网站上找到Unity项目教程,以便您轻松开始此处 。
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.
解决方案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. TheFindObjectOfType
function is used to find this type of script only. Let's say that we have a script calledSceneLoader
and we passSceneLoader
to theSingleton
class, it will check if an instance ofSceneLoader
is already in the scene and return that instance of it. It will returnnull
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.
这篇关于如何在Unity中创建一个通用的单例类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!