using UnityEngine; using UnityEngine.UI; namespace Superlazy.UI { public class SLUIFade : SLUIOnOffTransition { public float fadeInTime = 1.0f; public float fadeOutTime = 1.0f; private Graphic[] graphics; private float currentAlpha = 0.0f; protected override void Validate() { } protected override void Init() { graphics = GetComponentsInChildren(); if (currentState) currentAlpha = 1.0f; else currentAlpha = 0.0f; foreach (var graphic in graphics) { var c = graphic.color; c.a = currentAlpha; graphic.color = c; } } protected override bool OnUpdate(bool forceUpdate = false) { var result = false; if (currentState) { if (fadeInTime != 0) currentAlpha += Time.unscaledDeltaTime / fadeInTime; else currentAlpha = 1.0f; if (currentAlpha >= 1.0f) { currentAlpha = 1.0f; result = true; } } else { if (fadeOutTime != 0) currentAlpha -= Time.unscaledDeltaTime / fadeOutTime; else currentAlpha = 0.0f; if (currentAlpha <= 0.0f) { currentAlpha = 0.0f; result = true; } } foreach (var graphic in graphics) { var c = graphic.color; c.a = currentAlpha; graphic.color = c; } return result; } protected override void ForceDisable() { foreach (var graphic in graphics) { var c = graphic.color; c.a = 0; graphic.color = c; } } } }