블로그 이미지
Every unexpected event is a path to learning for you. blueasa

카테고리

분류 전체보기 (2794)
Unity3D (852)
Programming (478)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (234)
협업 (11)
3DS Max (3)
Game (12)
Utility (68)
Etc (98)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (55)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday

Singleton

Unity3D/Script / 2014. 3. 5. 10:41

일반형 Singleton

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class MySingleton
{
    private static MySingleton instance;
 
    //
    private MySingleton()
    {
 
    }
 
    static MySingleton()
    {
        instance = new MySingleton();
    }
 
    public static MySingleton Instance
    {
        get
        {
            return instance;
        }
    }
};

Component용 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using UnityEngine;
using System.Collections;
 
public class MySingleton : MonoBehaviour {
 
    private static MySingleton instance;
     
    //
    private MySingleton()
    {
    }
     
    //
    public static MySingleton Instance
    {
        get
        {
            if (instance == null)
            {
                instance =  FindObjectOfType(typeof (MySingleton)) as MySingleton;
             
                if (instance == null)
                {
                    GameObject obj = new GameObject("MySingleton");
                    instance = obj.AddComponent(typeof (MySingleton)) as MySingleton;
                    Debug.Log ("Could not locate an MySingleton object. MySingleton was Generated Automaticly.");
                }
            }
  
            return instance;
        }
    }
     
    void Awake()
    {
        DontDestroyOnLoad(this);
    }
     
    void OnApplicationQuit()
    {
        instance = null;
    }
}

제네릭 버전

일반형 Singleton

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class  Singleton<T> where T : class, new()
{
    private static T instance;
         
    static Singleton()
    {
        instance = new T();
    }
     
    public static T Instance
    {
        get
        {
            return instance;
        }
    }
};

Component용 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using UnityEngine;
using System.Collections;
  
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
    private static T instance;
     
    //
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance =  FindObjectOfType(typeof(T)) as T;
             
                if (instance == null)
                {
                    GameObject obj = new GameObject(typeof (T).ToString());
                    instance = obj.AddComponent(typeof (T)) as T;
                    instance.Init();
         
                    Debug.Log ("Could not locate an " + typeof (T).ToString() +" object. " + typeof (T).ToString() + " was Generated Automaticly.");
                }
            }
         
            return instance;
        }
    }
      
    void Awake()
    {
        if (instance == null)
        {
            instance = this as T;
            instance.Init();
        }
         
        DontDestroyOnLoad(this);
    }
      
    void OnApplicationQuit()
    {
        instance = null;
    }
     
    public virtual void Init()
    {
         
    }
}



반응형
Posted by blueasa
, |