Unity - SingleTon

제네릭 타입 싱글톤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System.Collections;
using UnityEngine;
 
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
 
    private static T _instance;
 
    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType(typeof(T)) as T;
 
                if (_instance == null)
                {
                    Debug.LogError("error");
                }
            }
            return _instance;
        }
    }
}
 
cs

댓글