48 lines
1.1 KiB
C#
48 lines
1.1 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
public abstract class SingletonMonoBehaviour<T> : MonoBehaviour where T : MonoBehaviour {
|
|||
|
|
|
|||
|
|
private static T instance;
|
|||
|
|
public static T Instance {
|
|||
|
|
get{
|
|||
|
|
FindInstance();
|
|||
|
|
if(instance == null){
|
|||
|
|
Debug.LogError(typeof(T) + " is nothing.");
|
|||
|
|
#if UNITY_EDITOR
|
|||
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
return instance;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public virtual void OnDestroy(){
|
|||
|
|
if(instance == this) instance = null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected bool CheckInstance(){
|
|||
|
|
if(this != Instance){
|
|||
|
|
#if UNITY_EDITOR
|
|||
|
|
Debug.LogError(typeof(T) + " is already exists.");
|
|||
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|||
|
|
#endif
|
|||
|
|
Destroy(this);
|
|||
|
|
return true;
|
|||
|
|
}else{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static void FindInstance(){
|
|||
|
|
if(instance == null){
|
|||
|
|
instance = (T)FindObjectOfType(typeof(T));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static bool ExistsInstance{
|
|||
|
|
get{
|
|||
|
|
FindInstance();
|
|||
|
|
return instance != null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|