32 lines
		
	
	
		
			755 B
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			32 lines
		
	
	
		
			755 B
		
	
	
	
		
			C#
		
	
	
	
| using UnityEngine;
 | |
| using System;
 | |
| 
 | |
| public class TGSingleton<T> : MonoBehaviour where T : class
 | |
| {
 | |
|     
 | |
|     private static T _instance;
 | |
|     public static T Instance
 | |
|     {
 | |
|         get
 | |
|         {
 | |
|             if (_instance == null)
 | |
|             {
 | |
|                 var previous = FindObjectOfType(typeof(T));
 | |
|                 if (previous)
 | |
|                 {
 | |
|                     _instance = previous as T;
 | |
|                 }
 | |
|                 else
 | |
|                 {
 | |
|                     string name = typeof(T).ToString();
 | |
|                     var go = new GameObject("__"+name);
 | |
|                     _instance = go.AddComponent(typeof(T)) as T;
 | |
|                     DontDestroyOnLoad(go);
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             return _instance;
 | |
|         }
 | |
|     }
 | |
| 
 | |
| } |