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

카테고리

분류 전체보기 (2731)
Unity3D (814)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (57)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (51)
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
03-29 00:00

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
, |