📒 갈무리 - Singleton Pattern
📌 Singleton Pattern이란?
- 메모리상으로 단 하나만 존재하며 언제 어디서든 사용할 수 있는 오브젝트를 만들 때 사용되는 패턴
 
- 보통 게임 개발에서는 매니저, 관리자, 핸들러, 헬퍼 등에 사용된다.
 
📌 Singleton Pattern 예제
일반적인 경우
public class ScoreManager : MonoBehaviour
{
    public int score = 0;
    public int GetScore()
    {
        return score;
    }
    public void AddScore(int newScore)
    {
        score = score + newScore;
    }
}
public class ScoreAdder : MonoBehaviour
{
	
    public ScoreManager scoreManager;
    void Update()
    {
        
        if (Input.GetMouseButtonDown(0))
        {
            
            
            scoreManager.AddScore(5);
            
            Debug.Log(scoreManager.GetScore());
        }
    }
}
 
싱글톤 패턴 적용
public class ScoreManager : MonoBehaviour
{
    
    
    public static ScoreManager instance;
    private int score = 0;
	
    private void Awake()
    {
        
        instance = this;
    }
    public int GetScore()
    {
        return score;
    }
    public void AddScore(int newScore)
    {
        score = score + newScore;
    }
}
public class ScoreAdder : MonoBehaviour
{
    
    
    void Update()
    {
        
        if (Input.GetMouseButtonDown(0))
        {
            
            
            ScoreManager.instance.AddScore(5);
            
            Debug.Log(ScoreManager.instance.GetScore());
        }
    }
}
 
객체 null 체크
	private static ScoreManager instance;
    
    
    public static ScoreManager GetInstance()
    {
        
        
        if(instance == null)
        {
            instance = FindObjectOfType<ScoreManager>();
        }
        return instance;
    }
	
	private static ScoreManager instance;
    
    public static ScoreManager GetInstance()
    {
        if (instance == null)
        {
            instance = FindObjectOfType<ScoreManager>();
            
            if (instance == null)
            {
                
                
                GameObject container = new GameObject("Score Manager");
                
                instance = container.AddComponent<ScoreManager>();
            }
        }
        return instance;
    }
 
싱글톤 클래스를 만들어 놓고, 그 클래스를 이용하여 원하는 Component를 싱글톤 객체로 생성
public class SingletonMonobehaviour<T> : MonoBehaviour where T : Component
{
    private static T m_instance;
    public static T Instance => m_instance; 
    
    public static T GetOrCreateInstance()
    {
        if(m_instance == null)
        {
            m_instance = (T)FindObjectOfType(typeof(T));
            if (m_instance == null)
            {
                GameObject _newGameObject = new GameObject(typeof(T).Name, typeof(T));
                m_instance = _newGameObject.GetComponent<T>();
            }
        }
        return m_instance;
    }
    protected virtual void Awake()
    {
        
        m_instance = this as T;
        if (Application.isPlaying == true)
        {
            
            
            
            DontDestroyOnLoad(gameObject);
        }
    }
}
public class AceUtils : SingletonMonobehaviour<AceUtils>