본문 바로가기

Programming/Game Engine

[Unity] Unity Singleton 사용

728x90
반응형

유니티에서 싱글톤 패턴 사용



public class Singleton : MonoBehaviour {


    private static Singleton _instance = null;


    public static Singleton GetInstance()

    {

        if (_instance == null)

        {

            _instance = FindObjectOfType(typeof(Singleton)) as SingletonDataClass;

            if (_instance == null)

            {

                Debug.LogError("There's no active Singleton Class ");

                GameObject container = new GameObject();

                container.name = "MyClassContainer";

                _instance = container.AddComponent(typeof(Singleton)) as Singleton;

            }

        }


        return _instance;

    }


    // Variable space


    // Function scace



반응형