본문 바로가기
프로그래밍/디자인패턴

Singleton Pattern_싱글톤_유니티(Unity)에서 사용가능한 5가지 싱글톤_유니티(C#)로 사용해 보는 디자인 패턴

by 리뷰하는 (게임)프로그래머_리프TV 2020. 6. 6.

유니티에서 활용 가능한 5가지 싱글톤 패턴에 대해서 알아보자

 

1. 일반적인 싱글톤

 

2. 모노(MonoBehaviour)를 사용한 싱글톤

 

3. 프로퍼티(Property)를 사용한 싱글톤

 

4. 제네릭을 활용한 싱글톤

 4-1. 모노(MonoBehaviour)를 사용 하지 않는 싱글톤

 4-2. 모노(MonoBehaviour)를 사용 하는 싱글톤

 

 

1. 일반적인 싱글톤

using UnityEngine;

public class SingletonPattern
{
    private SingletonPattern() { }
    private static SingletonPattern instance = null;
    public static SingletonPattern GetInstance()
    {
        if (instance == null)
        {
            instance = new SingletonPattern();
        }

        return instance;
    }

    public void Use()
    {
        Debug.Log("일반적인 싱글톤");
    }
}

 

사용

SingletonPattern.GetInstance().Use();

 

2. 모노(MonoBehaviour)를 사용한 싱글톤

using UnityEngine;

public class SingletonPattern_Mono : MonoBehaviour
{
    private SingletonPattern_Mono() { }
    private static SingletonPattern_Mono instance = null;
    public static SingletonPattern_Mono GetInstance()
    {
        if (instance == null)
        {
            Debug.LogError("해당 오브젝트가 없다");
        }

        return instance;
    }

    public void Awake()
    {
        instance = this;
    }

    public void Use()
    {
        Debug.Log("모노를 사용한 싱글톤");
    }

}

 

사용

SingletonPattern_Mono.GetInstance().Use();

주의

* 해당 GameObject가 Hierachy에 있어야 한다. 

 

 

3. 프로퍼티(Property)를 사용한 싱글톤

using UnityEngine;

public class SingletonPattern_Property
{
    private SingletonPattern_Property() { }

    private static SingletonPattern_Property instance = null;
    public static SingletonPattern_Property Instance
    {
        get
        {
            if (instance == null)
                instance = new SingletonPattern_Property();
            return instance;
        }
        private set
        {
            instance = value;
        }
    }

    public void Use()
    {
        Debug.Log("프로퍼티를 사용한 싱글톤");
    }

}

 

사용

SingletonPattern_Property.Instance.Use();

 

설명

함수를 사용한게 아니기 때문에 일반적인 싱글톤보다 조금 더 참조는 편함

 

 

4. 제네릭을 활용한 싱글톤

 4-1. 모노(MonoBehaviour)를 사용 하지 않는 싱글톤

using System;

public class SingletonPattern_IsA<T> where T : class
{
    private static T instance = null;
    public static T Instnace
    {
        get
        {
            if (instance == null)
            {
                instance = Activator.CreateInstance(typeof(T)) as T;
            }
            return instance;
        }
    }
}

C# 제너릭으로 구현 상속을 꼭 해야한다.

 

상속 예제

using UnityEngine;

public class ItemManager : SingletonPattern_IsA<ItemManager>
{ 
    public void Use()
    {
        Debug.Log("제너릭을 사용한 싱글톤");
    }
}

 

사용

ItemManager.Instnace.Use();

 

 

 4-2. 모노(MonoBehaviour)를 사용 하는 싱글톤

using UnityEngine;

public class SingletonPattern_IsA_Mono<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance = null;
    public static T Insatnce
    {
        get
        {
            instance = FindObjectOfType(typeof(T)) as T;

            if (instance == null)
            {
                instance = new GameObject(typeof(T).ToString(), typeof(T)).AddComponent<T>();
            }
            return instance;
        }
    }

}

 

 

상속 예제

using UnityEngine;

public class SkillManager : SingletonPattern_IsA_Mono<SkillManager>
{
    public void Use()
    {
        Debug.Log("모노 제너릭을 사용한 싱글톤");
    }
}

 

사용

SkillManager.Insatnce.Use();

 

정리

정답은 없다고 생각한다.

상황에 따라 조금씩 스타일을 변경 할 수 있겠지만

각 싱글톤이 언제 어느 타이밍에 써야 좋은지 고민해서 사용하면 좋을 것이라고 생각한다.