Beispiel #1
0
        /// <summary>
        /// Register a method, so that it will be run each time the counter lasted for specified duration
        /// </summary>
        /// <param name="callback">The method to call, can't be null</param>
        /// <param name="duration">How long before the callback is call</param>
        /// <param name="loop">Default is true, if false, the callback will be removed after 1 run</param>
        public void RegisterForDuration(Callback callback, int duration, bool loop = true)
        {
            if (!isInitialized)
            {
                Initialize();
            }

            if (callback == null)
            {
                Debug.LogWarning("Trying to add null as callback for TimeCounter, will ignore this call");
                return;
            }

            TimeCounterItem item = null;

            //if the system has contain list of callback at this duration
            if (subscribers.ContainsKey(duration))
            {
                //try to find a counter that hasn't start counting yet. Why?
                //Because if it's counting, the duration will became not accuracy anymore
                for (int i = 0; i < subscribers[duration].Count; i++)
                {
                    if (subscribers[duration][i].elapsedTime == 0)
                    {
                        item = subscribers[duration][i];
                    }
                }

                //if there is non to be found, create new one
                if (item == null)
                {
                    item = new TimeCounterItem();
                    subscribers[duration].Add(item);
                }
            }
            else
            {
                var list = new List <TimeCounterItem>(10);
                item = new TimeCounterItem();
                list.Add(item);
                subscribers.Add(duration, list);
            }

            if (loop)
            {
                item.listLoopCallbacks.Add(callback);
            }
            else
            {
                item.listOneTimeCallbacks.Add(callback);
            }
        }
Beispiel #2
0
        public void UnregisterForDuration(Callback callback, int duration, bool isLoop = true)
        {
            if (callback == null)
            {
                Debug.LogWarning("Trying to remove null as callback for TimeCounter, will ignore this call");
                return;
            }

            if (subscribers.ContainsKey(duration))
            {
                for (int j = 0; j < subscribers[duration].Count; j++)
                {
                    TimeCounterItem item = subscribers[duration][j];
                    List <Callback> list = isLoop ? item.listLoopCallbacks : item.listOneTimeCallbacks;
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i] == callback)
                        {
                            list.RemoveAt(i--);
                        }
                    }
                }
            }
        }