diff --git a/Assets/Scripts/StandAlone/UI/FadeInOut.cs b/Assets/Scripts/StandAlone/UI/FadeInOut.cs index 4843fa9173eb2e4164c1686bbcddb187e998b79e..c7b5903afde781c8b543efbf889a5ddda09126d7 100644 --- a/Assets/Scripts/StandAlone/UI/FadeInOut.cs +++ b/Assets/Scripts/StandAlone/UI/FadeInOut.cs @@ -1,4 +1,6 @@ -using System.Collections; +using System; +using System.Collections; +using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; @@ -12,11 +14,32 @@ public class FadeInOut : MonoBehaviour public bool startHidden; [Header("Autogenerated values")] - public Image[] imageTargets; + public Dictionary<Image, AlphaData> imageAlphas; - public TMP_Text[] textTargets; + public Dictionary<TMP_Text, AlphaData> textAlphas; - public MeshRenderer[] meshTargets; + public Dictionary<MeshRenderer, AlphaData> meshAlphas; + + [System.Serializable] + public class AlphaData + { + public float baseValue; + public float percent; + + public float AdjValue => baseValue * percent; + + public AlphaData(float baseValue, float percent) + { + this.baseValue = baseValue; + this.percent = percent; + } + + public Color CalculateColor(Color input) + { + input.a = AdjValue; + return input; + } + } public enum State { @@ -32,9 +55,13 @@ public class FadeInOut : MonoBehaviour private void Start() { - imageTargets = GetComponentsInChildren<Image>(true); - textTargets = GetComponentsInChildren<TMP_Text>(true); - meshTargets = GetComponentsInChildren<MeshRenderer>(true); + var imageTargets = GetComponentsInChildren<Image>(true); + var textTargets = GetComponentsInChildren<TMP_Text>(true); + var meshTargets = GetComponentsInChildren<MeshRenderer>(true); + + imageAlphas = imageTargets.ToDictionary(i => i, i => new AlphaData(i.color.a, 0)); + textAlphas = textTargets.ToDictionary(t => t, t => new AlphaData(t.color.a, 0)); + meshAlphas = meshTargets.ToDictionary(m => m, m => new AlphaData(m.material.color.a, 0)); SetTargetsAlpha(startHidden ? 0 : 1); FadeState = startHidden ? State.FadedOut : State.FadedIn; @@ -80,27 +107,30 @@ public class FadeInOut : MonoBehaviour } } - private void SetTargetsAlpha(float alpha) + private void SetTargetsAlpha(float percent) { - foreach (var target in imageTargets) + foreach (var pair in imageAlphas) { - Color targetColor = target.color; - targetColor.a = alpha; - target.color = targetColor; + var a = pair.Value; + var i = pair.Key; + a.percent = percent; + i.color = a.CalculateColor(i.color); } - foreach (var target in textTargets) + foreach (var pair in textAlphas) { - Color targetColor = target.color; - targetColor.a = alpha; - target.color = targetColor; + var a = pair.Value; + var t = pair.Key; + a.percent = percent; + t.color = a.CalculateColor(t.color); } - foreach (var target in meshTargets) + foreach (var pair in meshAlphas) { - Color targetColor = target.material.color; - targetColor.a = alpha; - target.material.color = targetColor; + var a = pair.Value; + var m = pair.Key; + a.percent = percent; + m.material.color = a.CalculateColor(m.material.color); } }