Singleton
This commit is contained in:
parent
5d2ce2ae7e
commit
26b316d431
8
Assets/Singleton.meta
Normal file
8
Assets/Singleton.meta
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 46c57a5edc113954c92ea81e20be4682
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
8
Assets/Singleton/Scripts.meta
Normal file
8
Assets/Singleton/Scripts.meta
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 79590437eab636d45b613b2bcda6a904
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
93
Assets/Singleton/Scripts/UnitySingleton.cs
Normal file
93
Assets/Singleton/Scripts/UnitySingleton.cs
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace FirstVillain.Singleton
|
||||||
|
{
|
||||||
|
public class UnitySingleton<T> : MonoBehaviour where T : UnityEngine.Component
|
||||||
|
{
|
||||||
|
private static T _instance = null;
|
||||||
|
|
||||||
|
public static T Instance
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_instance == null)
|
||||||
|
{
|
||||||
|
string name = (typeof(T)).ToString();
|
||||||
|
_instance = new GameObject(name).AddComponent<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void Awake()
|
||||||
|
{
|
||||||
|
if (_instance != null && _instance != this)
|
||||||
|
{
|
||||||
|
Destroy(gameObject);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_instance = this as T;
|
||||||
|
}
|
||||||
|
|
||||||
|
DontDestroyOnLoad(gameObject);
|
||||||
|
|
||||||
|
AwakeSingleton();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDestroy()
|
||||||
|
{
|
||||||
|
if (_instance == this)
|
||||||
|
{
|
||||||
|
_instance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void AwakeSingleton()
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
public class UnitySingletonOnce<T> : MonoBehaviour where T : UnityEngine.Component
|
||||||
|
{
|
||||||
|
private static T _instance = null;
|
||||||
|
|
||||||
|
public static T Instance
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_instance == null)
|
||||||
|
{
|
||||||
|
string name = (typeof(T)).ToString();
|
||||||
|
_instance = new GameObject(name).AddComponent<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void Awake()
|
||||||
|
{
|
||||||
|
if (_instance != null && _instance != this)
|
||||||
|
{
|
||||||
|
Destroy(gameObject);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_instance = this as T;
|
||||||
|
}
|
||||||
|
|
||||||
|
AwakeSingleton();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDestroy()
|
||||||
|
{
|
||||||
|
if (_instance == this)
|
||||||
|
{
|
||||||
|
_instance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void AwakeSingleton()
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Singleton/Scripts/UnitySingleton.cs.meta
Normal file
11
Assets/Singleton/Scripts/UnitySingleton.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 777a878dd9f43e349ac20fcd7e63dc31
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user