프로젝트

일반

사용자정보

통계
| 개정판:

t1 / TFDContents / Assets / Standard Assets / EventPump.cs @ 9

이력 | 보기 | 이력해설 | 다운로드 (2.11 KB)

1
#if !(UNITY_WSA_10_0 && NETFX_CORE)
2
using System;
3
using System.Collections.Generic;
4
using System.Runtime.InteropServices;
5
using System.Linq;
6

    
7
namespace Helper
8
{
9
    internal class EventPump : UnityEngine.MonoBehaviour
10
    {
11
        private static object s_Lock = new object();
12
        private Queue<Action> m_Queue = new Queue<Action>();
13

    
14
        public static EventPump Instance
15
        {
16
            get;
17
            private set;
18
        }
19

    
20
        public static void EnsureInitialized()
21
        {
22
            try
23
            {
24
                if (EventPump.Instance == null)
25
                {
26
                    lock (s_Lock)
27
                    {
28
                        if (EventPump.Instance == null)
29
                        {
30
                            UnityEngine.GameObject parent = new UnityEngine.GameObject("Kinect Desktop Event Pump");
31
                            EventPump.Instance = parent.AddComponent<Helper.EventPump>();
32
                            DontDestroyOnLoad(parent);
33
                        }
34
                    }
35
                }
36
            }
37
            catch
38
            {
39
                UnityEngine.Debug.LogError("Events must be registered on the main thread.");
40
                return;
41
            }
42
        }
43

    
44
        private void Update()
45
        {
46
            lock (m_Queue)
47
            {
48
                while (m_Queue.Count > 0)
49
                {
50
                    var action = m_Queue.Dequeue();
51
                    try
52
                    {
53
                        action.Invoke();
54
                    }
55
                    catch { }
56
                }
57
            }
58
        }
59

    
60
        private void OnApplicationQuit()
61
        {
62
            var sensor = Windows.Kinect.KinectSensor.GetDefault();
63
            if (sensor != null && sensor.IsOpen)
64
            {
65
                sensor.Close();
66
            }
67

    
68
            NativeObjectCache.Flush();
69
        }
70

    
71
        public void Enqueue(Action action)
72
        {
73
            lock (m_Queue)
74
            {
75
                m_Queue.Enqueue(action);
76
            }
77
        }
78
    }
79
}
80
#endif