728x90
//예제1
private static T _instance = null;
public static T _Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType(typeof(T)) as T;
}
return _instance;
}
}
//예제2
private static T _instance = null;
public static T _Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType(typeof(T)) as T;
if (_instance == null)
{
GameObject obj = new GameObject();
obj.name = "NAME";
_instance = obj.AddComponent<T>();
}
}
return _instance;
}
}
//예제3
public class Singleton<T> where T : class, new()
{
public static readonly T Instance = new T();
}
//예제4
public class Singleton<T> where T : class, new()
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
_instance = new T();
return _instance;
}
}
public virtual void Init() { }
public static void SetInstance(T instance)
{
_instance = instance;
}
}
728x90
반응형
'Unity3D > Unity & C#' 카테고리의 다른 글
Unity & C# ~ Button Sprite Swap Scirpt Controller (0) | 2022.05.31 |
---|---|
Unity & C# ~ 리모트 버튼에서 스킵 버튼을 눌렀을 때 동작 (0) | 2022.05.31 |
Unity & C# ~ String.Format 자리수 (0) | 2022.04.27 |
Unity & C# ~ Property 프로퍼티 (0) | 2022.04.27 |
Atlas에 들어있는 Sprite Load (0) | 2022.04.22 |