// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using System; using System.Collections; using UltimateXR.Animation.Interpolation; using UnityEngine; namespace UltimateXR.Extensions.Unity { /// /// extensions. /// public static class MonoBehaviourExt { #region Public Methods /// /// Enables/disabled the component if it isn't enabled already. /// /// Component to enable/disable /// Whether to enable or disable the component public static void CheckSetEnabled(this MonoBehaviour self, bool enable) { if (self.enabled != enable) { self.enabled = enable; } } #endregion #region Coroutines /// /// Creates a coroutine that simplifies executing a loop during a certain amount of time. /// /// Caller /// Time in seconds of the interpolation /// /// The action to perform on each loop step. The action receives /// the interpolation value t [0.0, 1.0] as parameter. /// /// Easing to use in the interpolation (linear by default) /// Forces a last loop step with t = 1.0f exactly. /// Coroutine enumerator public static IEnumerator LoopCoroutine(this MonoBehaviour monoBehaviour, float durationSeconds, Action loopAction, UxrEasing easing = UxrEasing.Linear, bool forceLastT1 = false) { float startTime = Time.time; while (Time.time - startTime < durationSeconds) { float t = UxrInterpolator.Interpolate(0.0f, 1.0f, Time.time - startTime, new UxrInterpolationSettings(durationSeconds, 0.0f, easing)); loopAction(t); yield return null; } if (forceLastT1) { loopAction(1.0f); } } #endregion } }