Unity3D Singleton

case : 1

using UnityEngine;
using System.Collections;

public class Singleton: MonoBehaviour
{
    public static Singleton Instance;             
    //Awake is always called before any Start functions
    void Awake()
    {
        if (Instance)
        {
             DestroyImmediate(gameObject);
        }
        else
        {
             Instance = this;
             DontDestroyOnLoad(gameObject);

        }
    }
}
case 2 :

public class MyClass  
{  
        private static MyClass _instance;  
        public static MyClass Instance  
        {  
                get  
                {  
                        if (!_instance)  
                        {  
                                _instance = GameObject.FindObjectOfType(typeof(MyClass));  
                                if (!_instance)  
                                {  
                                        GameObject container = new GameObject();  
                                        container.name = "MyClassContainer";  
                                        _instance = container.AddComponent(typeof(MyClass)) as MyClass;  
                                }  
                        }  
  
                        return _instance;  
                }  
        }  
}  

댓글

이 블로그의 인기 게시물

유니티 오브젝트 서서히 사라지게

WebView에서 YOUTUBE 동영상 플레이 방법