Exemple #1
0
        /// <summary>
        /// Stops all Tracked Coroutines with the provided tag on the provided Object
        /// </summary>
        /// <param name="obj">he Object that is affected by the Coroutine</param>
        /// <param name="tag">Tag of the Coroutine</param>
        /// <typeparam name="T"></typeparam>
        public static void StopAllTrackedCoroutines <T>(T obj, string tag)
        {
            // if there are no coroutines with the provided tag, do nothing
            if (!_coroutines.ContainsKey(tag) || _coroutines[tag].Count == 0)
            {
                return;
            }

            // in this list we save which routines need to be removed from our list
            List <CoroutineObjectPair> toRemove = new List <CoroutineObjectPair>();

            // go through all tracked coroutines with this tag and find the proper one
            foreach (CoroutineObjectPair pair in _coroutines[tag])
            {
                // try to cast the pair
                CoroutineObjectPair <T> p = pair as CoroutineObjectPair <T>;

                // if the cast was successfull and the Object in this pair is the provided object, we need to stop the coroutine and remember it to remove it from our list
                if (p != null && p.Obj.Equals(obj))
                {
                    Instance.StopCoroutine(p.Routine);
                    toRemove.Add(pair);
                }
            }

            // remove everything from the list
            foreach (CoroutineObjectPair pair in toRemove)
            {
                _coroutines[tag].Remove(pair);
            }

            toRemove = null;
        }
Exemple #2
0
        // ######################## COROUTINES ######################## //
        /// <summary>
        /// This is a wrapper coroutine for a tracked coroutine. It removes the routine from the list as soon as it finished
        /// </summary>
        /// <param name="routine">The Coroutine to run</param>
        /// <param name="cop">The CoroutineObjectPair that is associated to this Coroutine</param>
        /// <param name="t">The Tag of the Coroutine</param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private IEnumerator TrackRoutine <T>(IEnumerator routine, CoroutineObjectPair cop, string t)
        {
            Coroutine co = StartCoroutine(routine);

            cop.Routine = co;

            yield return(co);

            _coroutines[t].Remove(cop);
        }
Exemple #3
0
        /// <summary>
        /// If there is a tracked coroutine with the provided Tag on the provided object, it will be stopped
        /// </summary>
        /// <param name="obj">The Object that is affected by the Coroutine</param>
        /// <param name="tag">Tag of the Coroutine</param>
        /// <typeparam name="T"></typeparam>
        public static void StopTrackedCoroutine <T>(T obj, string tag)
        {
            // if there are no coroutines with the provided tag, do nothing
            if (!_coroutines.ContainsKey(tag) || _coroutines[tag].Count == 0)
            {
                return;
            }

            // go through all tracked coroutines with this tag and find the proper one
            foreach (CoroutineObjectPair pair in _coroutines[tag])
            {
                // try to cast the pair
                CoroutineObjectPair <T> p = pair as CoroutineObjectPair <T>;

                // if the cast was successfull and the Object in this pair is the provided object, we need to stop the coroutine and remove it from our list
                if (p != null && p.Obj.Equals(obj))
                {
                    Instance.StopCoroutine(p.Routine);
                    _coroutines[tag].Remove(pair);
                    return;
                }
            }
        }
Exemple #4
0
        // ######################## FUNCTIONALITY ######################## //
        /// <summary>
        /// Starts a Coroutine on the provided Object and remembers it so it can be stopped later
        /// </summary>
        /// <param name="routine">The Coroutine to start</param>
        /// <param name="obj">The Object that is affected by the Coroutine</param>
        /// <param name="tag">Tag of the Coroutine</param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Coroutine StartTrackedCoroutine <T>(IEnumerator routine, T obj, string tag = "Default")
        {
            if (_coroutines == null)
            {
                _coroutines = new Dictionary <string, List <CoroutineObjectPair> >();
            }
            // if this tag does not exist yet, create an entry for it
            if (!_coroutines.ContainsKey(tag))
            {
                _coroutines.Add(tag, new List <CoroutineObjectPair>());
            }


            // create the pair and add it to the list
            CoroutineObjectPair <T> cop = new CoroutineObjectPair <T>(null, obj);

            _coroutines[tag].Add(cop);

            // start the coroutine
            Coroutine co = Instance.StartCoroutine(Instance.TrackRoutine <T>(routine, cop, tag));

            // return the tracked routine
            return(co);
        }