/// <summary> /// Smoothly switch the visibility of the menu. /// </summary> /// <param name="menusType">The visibility of the menu needs to be set.</param> /// <param name="visibilityState">The visibility state will set to the menu's visibility.</param> /// <param name="smoothlTime">The time of the smoothly transition.</param> public void SetMenuVisibilitySmoothly(Global.MenusType menusType, bool visibilityState, float smoothlTime = 1.0f, bool slowToFast = true) { if (menusVisibilityState[(int)menusType] != visibilityState) { menusVisibilityState[(int)menusType] = visibilityState; //StopAllCoroutines(); StartCoroutine(VisibilitySmoothlySwitchCoroutine(menusType, menusVisibilityState[(int)menusType], smoothlTime, slowToFast)); } }
private IEnumerator VisibilitySmoothlySwitchCoroutine(Global.MenusType menusType, bool visibilityState, float smoothlTime, bool slowToFast = true) { float alpha = menusCanvasGroup[(int)menusType].alpha; // Get the latest alpha value when everytime invoking VisibilitySmoothlySwitchCoroutine() float time = (visibilityState ? alpha : 1.0f - alpha); bool visibility = visibilityState; if (menusVisibilityState[(int)menusType]) { menusCanvasGroup[(int)menusType].interactable = menusCanvasGroup[(int)menusType].blocksRaycasts = visibilityState; // Switch the menu interactable and the mouse raycast block before switching while open the menu } while (true) { if (slowToFast) { time += ((1.0f * Time.deltaTime) + (time * 0.2f)) / smoothlTime; } else { time += ((1.0f * Time.deltaTime) + ((1.0f - time) * 0.2f)) / smoothlTime; //(1.0f * Time.deltaTime) / smoothlTime; } time = (time > 1.0f) ? 1.0f : time; menusCanvasGroup[(int)menusType].alpha = Mathf.Lerp(visibilityState ? 0.0f : 1.0f, visibilityState ? 1.0f : 0.0f, time);// Smoothly switch the visibility if (visibility != menusVisibilityState[(int)menusType]) { break; // Stop visibility transition if the coroutine be invoking again } if (time >= 1.0f) { break; } yield return(null); } if (!menusVisibilityState[(int)menusType]) { menusCanvasGroup[(int)menusType].interactable = menusCanvasGroup[(int)menusType].blocksRaycasts = visibilityState; // Switch the menu interactable and the mouse raycast block after switched while close the menu } yield return(null); }
/// <summary> /// Directly switch the visibility of the menu. /// </summary> /// <param name="menusType">The visibility of the menu needs to be set.</param> /// <param name="visibilityState">The visibility state will set to the menu's visibility.</param> public void SetMenuVisibilityDirectly(Global.MenusType menusType, bool visibilityState) { menusVisibilityState[(int)menusType] = visibilityState; // Update the menus active state while switch the active of menu menusCanvasGroup[(int)menusType].interactable = menusCanvasGroup[(int)menusType].blocksRaycasts = visibilityState; // Switch the menu interactable and the mouse raycast block menusCanvasGroup[(int)menusType].alpha = visibilityState ? 1.0f : 0.0f; // Switch the visibility }
public void InverseMenuVisibilityDirectly(Global.MenusType menusType) { SetMenuVisibilityDirectly(menusType, !menusVisibilityState[(int)menusType]); }