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

카테고리

분류 전체보기 (2738)
Unity3D (817)
Script (91)
Extensions (14)
Effect (3)
NGUI (77)
UGUI (8)
Physics (2)
Shader (36)
Math (1)
Design Pattern (2)
Xml (1)
Tips (200)
Link (22)
World (1)
AssetBundle (25)
Mecanim (2)
Plugins (70)
Trouble Shooting (68)
Encrypt (7)
LightMap (4)
Shadow (4)
Editor (8)
Crash Report (3)
Utility (9)
UnityVS (2)
Facebook SDK (2)
iTween (3)
Font (11)
Ad (14)
Photon (2)
IAP (1)
Google (8)
Android (45)
iOS (41)
Programming (475)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
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
04-29 11:53
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
Singleton  (0) 2014.03.05
Serializable, NonSerialized  (0) 2013.07.30
Get MAC address on Android device  (0) 2013.07.24
Posted by blueasa
, |

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


링크 : http://blog.naver.com/khagaa/30128920649

반응형
Posted by blueasa
, |

링크 1: http://forum.unity3d.com/threads/188441-Retrieving-MAC-address-on-Android-device


링크 2: http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device/13007325#13007325

반응형
Posted by blueasa
, |

작업을 하다가 필요에 의해서 문득 든 궁금증..


컴포넌트로 추가 된 스크립트는 명시적인 자신의 이름으로만 찾아(GetComponent)지는가?


그래서 간단히 테스트 해봤다.


1) 클래스 2개 만들고,

public class Test : MonoBehaviour

{

    public int m_iCount = 0;   // 접근 테스트용

    ....

}


public class Test2 : Test

{

    ....

}


2) Test2를 Component로 추가

3) 이 상태에서 Test 클래스로 GetComponent를 해봤다.

Test scTest = gameObject.GetComponent<Test>();

if (scTest)

{

    Debug.Log(scTest.m_iCount);

}



[결론]

잘된다. 쓰자~

반응형
Posted by blueasa
, |


링크 : http://mingu.kr/80

반응형
Posted by blueasa
, |


링크 : http://www.unitystudy.net/bbs/board.php?bo_table=writings&wr_id=43

반응형
Posted by blueasa
, |
  1. using System.IO;
  2. DirectoryInfo dir = new DirectoryInfo(myPath);
  3. FileInfo[] info = dir.GetFiles("*.*");
  4. foreach (FileInfo f in info) 
  5. { ... }



참조 : http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx


출처 : http://answers.unity3d.com/questions/16433/get-list-of-all-files-in-a-directory.html

반응형
Posted by blueasa
, |

유니티엔진에서 일반적인 Timer()함수 대신에 MonoBehaviour 가 제공하는Invoke() 함수가 있습니다. 
Invoke(methodName:string, time:float) 
- methodName 메소드를 time 초 후 호출합니다. 

InvokeRepeating(methodName:string, time:float, repeatRate:float) 
- methodName 메소드를 time 초 후 호출합니다. 첫 호출 후 repeatRate 초 마다 반복 호출합니다. 
 InvokeRepeating 는 repeatRate 값이 0 보다 클때만 반복 호출됩니다. repeatRate 값이 0 일때는 최초 한번 호출 후 반복호출되지 않습니다.   ( 출처 : http://devkorea.co.kr/bbs/board.php?bo_table=m03_qna&wr_id=50255#c_50287 )


CancelInvoke() 
- 이 스크립트에 있는 모든 Invoke 를 취소합니다. 

CancelInvoke(methodName:string) 
- 이 스크립트에 있는 methodName 을 호출하는 모든 Invoke 를 취소합니다. 



출처 : http://devkorea.co.kr/bbs/board.php?bo_table=m03_qna&wr_id=31893&sca=&sfl=wr_subject%7C%7Cwr_content&stx=InvokeRepeating&sop=and&currentId=44



[참조]

http://blog.naver.com/bluefallsky/140190280479

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Invoke.html

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.CancelInvoke.html

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.IsInvoking.html

반응형
Posted by blueasa
, |




Link : http://www.richardfine.co.uk/2012/10/unity3d-monobehaviour-lifecycle/

반응형
Posted by blueasa
, |