예제 #1
0
        public static UnityTimer CreateTimer(Action timerCallback, TimeSpan timeSpan, bool isRepeating = true, bool autorun = true)
        {
            ArgumentValidator.AssertNotNull(timerCallback, "timerCallback");
            ArgumentValidator.AssertNotEquals(timeSpan, TimeSpan.Zero, "timeSpan", "timeSpan cannot be {0}", TimeSpan.Zero);

            const string timersHostName = "[TIMERS]";

            var go = GameObject.Find(timersHostName);

            if (go == null)
            {
                go = new GameObject(timersHostName)
                {
                    hideFlags = HideFlags.DontSave
                };
                Debug.LogFormat("Created {0}", go.name);
                if (Application.isPlaying)
                {
                    DontDestroyOnLoad(go);
                }
            }

            var timer = go.AddComponent <UnityTimer>();

            timer.name = string.Format("Timer_{0}_{1}", timerCallback, timerCallback.GetHashCode());

            timer.TimeInterval  = timeSpan;
            timer.IsRepeating   = isRepeating;
            timer.TimerCallBack = timerCallback;
            timer.Autorun       = autorun;
            Debug.LogFormat("Created new timer (name: {0}, autorun: {1}, repeating: {2}, interval: {3})", timer.name, autorun, isRepeating, timeSpan);

            return(timer);
        }