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

카테고리

분류 전체보기 (2093)
Unity3D (467)
Programming (453)
Unreal (2)
Gamebryo (56)
Tip & Tech (157)
협업 (26)
3DS Max (3)
Game (12)
Utility (98)
Etc (87)
Link (29)
Portfolio (18)
Subject (90)
iPhone (30)
Android (2)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (1)
Memories (16)
Interest (36)
Thinking (34)
한글 (23)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (15)
Exercise (3)
나만의 맛집 (2)
냥이 (7)
육아 (2)
Total1,077,061
Today279
Yesterday450
Statistics Graph

달력

« » 2017.08
    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    

공지사항

태그목록

Tistory Cumulus Flash tag cloud by BLUEnLIVE requires Flash Player 9 or better.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using UnityEngine;
 
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
    static T m_Instance = null;
 
    public static T instance
    {
        get
        {
            if( m_Instance != null )
            {
                return m_Instance;
            }
 
            System.Type type = typeof(T);
 
            T instance = GameObject.FindObjectOfType(type) as T;
 
            if( instance == null )
            {
                string typeName = type.ToString();
 
                GameObject gameObject = new GameObject( typeName, type );
                instance = gameObject.GetComponent<T>();
 
                if( instance == null )
                {
                    Debug.LogError("Problem during the creation of " + typeName,gameObject );
                }
            }
            else
            {
                Initialize(instance);
            }
            return m_Instance;
        }
    }
 
    static void Initialize(T instance)
    {
        if( m_Instance == null )
        {
            m_Instance = instance;
 
            m_Instance.OnInitialize();
        }
        else if( m_Instance != instance )
        {
            DestroyImmediate( instance.gameObject );
        }
    }
 
    static void Destroyed(T instance)
    {
        if( m_Instance == instance )
        {
            m_Instance.OnFinalize();
 
            m_Instance = null;
        }
    }
 
    public virtual void OnInitialize() {}
    public virtual void OnFinalize() {}
 
    void Awake()
    {
        Initialize( this as T );
    }
 
    void OnDestroy()
    {
        Destroyed( this as T );
    }
 
    void OnApplicationQuit()
    {
        Destroyed( this as T );
    }
}

使い方は

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using UnityEngine;
using System.Collections;
 
public class TestSingleton : MonoSingleton<TestSingleton>
{
    public override void OnInitialize()
    {
        Debug.Log ( "TestSingleton#OnInitialize" );
    }
 
    public override void OnFinalize()
    {
        Debug.Log ( "TestSingleton#OnFinalize" );
    }
}


출처 : http://caitsithware.com/wordpress/?p=118

저작자 표시 비영리 동일 조건 변경 허락
신고

'Unity3D > Script' 카테고리의 다른 글

Unity Singleton  (0) 2014.03.24
Platform Dependent Compilation  (0) 2014.03.11
Generic Based Singleton for MonoBehaviours完全版(?)  (0) 2014.03.05
Singleton  (0) 2014.03.05
Serializable, NonSerialized  (0) 2013.07.30
Get MAC address on Android device  (0) 2013.07.24
Posted by blueasa
TAG

댓글을 달아 주세요

티스토리 툴바