Ejemplo n.º 1
0
        /// <summary>
        /// Fade the main color from one color to another over time
        /// </summary>
        /// <param name="m">Material</param>
        /// <param name="mb">MonoBehaviour used for events</param>
        /// <param name="fromColor">From Color</param>
        /// <param name="toColor">To Color</param>
        /// <param name="seconds">Seconds</param>
        public static GmMonoBehaviourEventPromise Fade(this Material mat, MonoBehaviour mb, Color fromColor, Color toColor, float seconds, bool realtime)
        {
            if (seconds == 0)
            {
                mat.SetColor("_Color", toColor);
                var done = new GmMonoBehaviourEventPromise();
                done.Done();
                return(done);
            }

            float intervalSeconds = 0.1f;
            float step            = 0;
            int   fadeSteps       = (int)Math.Ceiling(seconds / intervalSeconds);

            //Debug.Log("Fade Steps = " + fadeSteps);

            mat.SetColor("_Color", fromColor);
            return(mb.Repeat(intervalSeconds, fadeSteps, () =>
            {
                step++;
                float timePercent = Mathf.Clamp(step / fadeSteps, 0, 1);
                //Debug.Log("Fade % = " + timePercent);
                mat.SetColor("_Color", Color.Lerp(fromColor, toColor, timePercent));
            }, realtime));
        }
        /// <summary>
        /// Fade the light intensity over time
        /// </summary>
        /// <param name="m">Material</param>
        /// <param name="mb">MonoBehaviour used for events</param>
        /// <param name="fromVolume">From Volume (0-1)</param>
        /// <param name="toVolume">To Volume (0-1)</param>
        /// <param name="seconds">Seconds</param>
        public static GmMonoBehaviourEventPromise FadeIntensity(this Light light, MonoBehaviour mb, float fromIntensity, float toIntensity, float seconds, bool realtime)
        {
            if (seconds == 0)
            {
                light.intensity = toIntensity;
                var done = new GmMonoBehaviourEventPromise();
                done.Done();
                return(done);
            }

            float intervalSeconds = 0.1f;
            float step            = 0;
            int   fadeSteps       = (int)Math.Ceiling(seconds / intervalSeconds);

            //Debug.Log("Fade Steps = " + fadeSteps);

            light.intensity = fromIntensity;
            return(mb.Repeat(intervalSeconds, fadeSteps, () =>
            {
                step++;
                float timePercent = Mathf.Clamp(step / fadeSteps, 0, 1);
                //Debug.Log("Fade % = " + timePercent);
                light.intensity = Mathf.Lerp(fromIntensity, toIntensity, timePercent);
            }));
        }
        /// <summary>
        /// Fade the alpha over time
        /// </summary>
        /// <param name="tmp"></param>
        /// <param name="fromAlpha"></param>
        /// <param name="toAlpha"></param>
        /// <param name="seconds"></param>
        /// <param name="realtime"></param>
        /// <returns></returns>
        public static GmMonoBehaviourEventPromise FadeAlpha(this TextMeshPro tmp, float fromAlpha, float toAlpha, float seconds, bool realtime)
        {
            if (seconds == 0)
            {
                tmp.SetAlpha(toAlpha);
                var done = new GmMonoBehaviourEventPromise();
                done.Done();
                return(done);
            }

            float intervalSeconds = 0.1f;
            float step            = 0;
            int   fadeSteps       = (int)Math.Ceiling(seconds / intervalSeconds);

            //Debug.Log("Fade Steps = " + fadeSteps);

            tmp.SetAlpha(fromAlpha);
            return(tmp.Repeat(intervalSeconds, fadeSteps, () =>
            {
                step++;
                float timePercent = Mathf.Clamp(step / fadeSteps, 0, 1);
                //Debug.Log("Fade % = " + timePercent);
                tmp.SetAlpha(Mathf.Lerp(fromAlpha, toAlpha, timePercent));
            }, true));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Fade the audio source volume over time
        /// </summary>
        /// <param name="m">Material</param>
        /// <param name="mb">MonoBehaviour used for events</param>
        /// <param name="fromVolume">From Volume (0-1)</param>
        /// <param name="toVolume">To Volume (0-1)</param>
        /// <param name="seconds">Seconds</param>
        public static GmMonoBehaviourEventPromise FadePitch(this AudioSource source, MonoBehaviour mb, float fromPitch, float toPitch, float seconds, bool realtime)
        {
            if (seconds == 0)
            {
                source.pitch = toPitch;
                var done = new GmMonoBehaviourEventPromise();
                done.Done();
                return(done);
            }

            float intervalSeconds = 0.1f;
            float step            = 0;
            int   fadeSteps       = (int)Math.Ceiling(seconds / intervalSeconds);

            //Debug.Log("Fade Steps = " + fadeSteps);

            source.pitch = fromPitch;
            return(mb.Repeat(intervalSeconds, fadeSteps, () =>
            {
                step++;
                float timePercent = Mathf.Clamp(step / fadeSteps, 0, 1);
                //Debug.Log("Fade % = " + timePercent);
                source.pitch = Mathf.Lerp(fromPitch, toPitch, timePercent);
            }, realtime));
        }
        public static GmMonoBehaviourEventPromise Delay(this MonoBehaviour mb, float delaySeconds, Action callback = null, bool realtime = false)
        {
            var promise = new GmMonoBehaviourEventPromise {
                monobehaviour = mb
            };

            if (mb != null && mb.isActiveAndEnabled)
            {
                promise.coroutine = mb.StartCoroutine(WaitThenCallback(mb, delaySeconds, 1, callback, promise, realtime));
            }
            else
            {
                promise.Abort();
            }
            return(promise);
        }
        private static IEnumerator WaitThenCallback(MonoBehaviour mb, float seconds, int times, Action callback, GmMonoBehaviourEventPromise promise, bool realTime)
        {
            for (var i = 0; i < times || times == int.MaxValue; i++)
            {
                if (realTime)
                {
                    yield return(new WaitForSecondsRealtime(seconds));
                }
                else
                {
                    yield return(new WaitForSeconds(seconds));
                }

                if (mb == null && true == false)
                {
                    promise.Abort();
                }
                else
                {
                    if (callback != null && mb.gameObject != null && mb.isActiveAndEnabled)
                    {
                        try
                        {
                            callback();
                        }
                        catch (Exception ex)
                        {
                            Debug.LogError("Callback Error: " + ex.Message);
                        }
                    }
                }
            }

            if (mb != null || true == true)
            {
                promise.Done();
            }
        }
Ejemplo n.º 7
0
 public GmMonoBehaviourEventPromiseAwaiter(GmMonoBehaviourEventPromise promise)
 {
     gmPromise = promise;
 }
Ejemplo n.º 8
0
 public static GmMonoBehaviourEventPromiseAwaiter GetAwaiter(this GmMonoBehaviourEventPromise promise)
 {
     return(new GmMonoBehaviourEventPromiseAwaiter(promise));
 }