Skip to content
Snippets Groups Projects
Commit 87df7099 authored by Ryan Chang's avatar Ryan Chang
Browse files

Updates to fade in out.

parent 4fa1c451
No related branches found
No related tags found
No related merge requests found
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);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment