using UnityEngine; using UnityEngine.UI; public class Fading : MonoBehaviour { public enum fade_type { Up = 0, Down = 1 } public float time_fade = 1f; public fade_type fade_Type; public float fadeUpValue = 1f; private void Start() { if (fade_Type == fade_type.Up) { SetAlpha(0f); FadeIn(); } if (fade_Type == fade_type.Down) { SetAlpha(1f); FadeOut(); } } private void OnEnable() { if (fade_Type == fade_type.Up) { SetAlpha(0f); FadeIn(); } if (fade_Type == fade_type.Down) { SetAlpha(1f); FadeOut(); } } public void StartFade(fade_type type, float time = 1f) { time_fade = time; if (type == fade_type.Up) { SetAlpha(0f); FadeIn(); } if (type == fade_type.Down) { SetAlpha(1f); FadeOut(); } } private void SetAlpha(float val) { CanvasRenderer[] componentsInChildren = GetComponentsInChildren(); for (int i = 0; i < componentsInChildren.Length; i++) { componentsInChildren[i].SetAlpha(val); } } private void FadeIn() { Image[] componentsInChildren = GetComponentsInChildren(); for (int i = 0; i < componentsInChildren.Length; i++) { componentsInChildren[i].CrossFadeAlpha(fadeUpValue, time_fade, ignoreTimeScale: true); } Text[] componentsInChildren2 = GetComponentsInChildren(); for (int j = 0; j < componentsInChildren2.Length; j++) { componentsInChildren2[j].CrossFadeAlpha(fadeUpValue, time_fade, ignoreTimeScale: true); } } private void FadeOut() { Image[] componentsInChildren = GetComponentsInChildren(); for (int i = 0; i < componentsInChildren.Length; i++) { componentsInChildren[i].CrossFadeAlpha(0f, time_fade, ignoreTimeScale: true); } Text[] componentsInChildren2 = GetComponentsInChildren(); for (int j = 0; j < componentsInChildren2.Length; j++) { componentsInChildren2[j].CrossFadeAlpha(0f, time_fade, ignoreTimeScale: true); } } }