48 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C#
		
	
	
	
| using UnityEngine;
 | |
| 
 | |
| namespace MMO
 | |
| {
 | |
|     public abstract class MMOSingleton<T> : MonoBehaviour where T : MMOSingleton<T>
 | |
|     {
 | |
|         private static T _instance = null;
 | |
| 
 | |
|         public static T Instance
 | |
|         {
 | |
|             get
 | |
|             {
 | |
|                 if (_instance == null)
 | |
|                 {
 | |
|                     GameObject go = new GameObject();
 | |
|                     DontDestroyOnLoad(go);
 | |
|                     go.name = "MMOSingleton:" + typeof(T).ToString();
 | |
|                     go.transform.localPosition = Vector3.zero;
 | |
|                     go.transform.localEulerAngles = Vector3.zero;
 | |
|                     go.transform.localScale = Vector3.one;
 | |
|                     _instance = go.AddComponent<T>();
 | |
|                 }
 | |
|                 return _instance;
 | |
|             }
 | |
|         }
 | |
|         private void Awake()
 | |
|         {
 | |
|             Initialize();
 | |
|         }
 | |
| 
 | |
|         protected virtual void Initialize()
 | |
|         {
 | |
| 
 | |
|         }
 | |
| 
 | |
|         private void OnDestroy()
 | |
|         {
 | |
|             _instance = null;
 | |
| 
 | |
|             Dispose();
 | |
|         }
 | |
| 
 | |
|         protected virtual void Dispose()
 | |
|         {
 | |
| 
 | |
|         }
 | |
|     }
 | |
| } |