Documentation for the Unity C# Library
Loading...
Searching...
No Matches
FadeEffect.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEngine;
5using UnityEngine.UI;
6
8{
12 public class FadeEffect : MonoBehaviour
13 {
14 [SerializeField] private RawImage _fadeRawImage;
15
16 public void Fade(FadeAction fadeAction,Action callback,Action centerCallback=null)
17 {
18 switch (fadeAction)
19 {
20 case FadeAction.FadeIn:
21 StartCoroutine(FadeIn(callback));
22 break;
23 case FadeAction.FadeOut:
24 StartCoroutine(FadeOut(callback));
25 break;
26 case FadeAction.FadeInAndOut:
27 StartCoroutine(FadeInAndOut(callback,centerCallback));
28 break;
29 case FadeAction.FadeOutAndIn:
30 StartCoroutine(FadeOutAndIn(callback));
31 break;
32 }
33 }
34
35 private IEnumerator FadeIn(Action callback)
36 {
37 for (float i = 0; i <= 1; i += Time.deltaTime)
38 {
39 _fadeRawImage.color = new Color(0, 0, 0, i);
40 yield return null;
41 }
42 callback?.Invoke();
43 _fadeRawImage.color = new Color(0, 0, 0, 1);
44 }
45
46 private IEnumerator FadeOut(Action callback)
47 {
48 for (float i = 1; i >= 0; i -= Time.deltaTime)
49 {
50 _fadeRawImage.color = new Color(0, 0, 0, i);
51 yield return null;
52 }
53 callback?.Invoke();
54 _fadeRawImage.color = new Color(0, 0, 0, 0);
55 }
56
57 private IEnumerator FadeInAndOut(Action callback,Action centerCallback)
58 {
59 for (float i = 0; i <= 1; i += Time.deltaTime)
60 {
61 _fadeRawImage.color = new Color(0, 0, 0, i);
62 yield return null;
63 }
64 centerCallback?.Invoke();
65 yield return new WaitForSeconds(1);
66
67 for (float i = 1; i >= 0; i -= Time.deltaTime)
68 {
69 _fadeRawImage.color = new Color(0, 0, 0, i);
70 yield return null;
71 }
72 _fadeRawImage.color = new Color(0, 0, 0, 0);
73 callback?.Invoke();
74 }
75
76 private IEnumerator FadeOutAndIn(Action callback)
77 {
78 for (float i = 1; i >= 0; i -= Time.deltaTime)
79 {
80 _fadeRawImage.color = new Color(0, 0, 0, i);
81 yield return null;
82 }
83 yield return new WaitForSeconds(1);
84
85 for (float i = 0; i <= 1; i += Time.deltaTime)
86 {
87 _fadeRawImage.color = new Color(0, 0, 0, i);
88 yield return null;
89 }
90 _fadeRawImage.color = new Color(0, 0, 0, 1);
91 callback?.Invoke();
92 }
93 }
94
95 public enum FadeAction
96 {
97 FadeIn,
101 }
102}
103
Main camera fade logic.
Definition FadeEffect.cs:13
IEnumerator FadeIn(Action callback)
Definition FadeEffect.cs:38
IEnumerator FadeOutAndIn(Action callback)
Definition FadeEffect.cs:79
RawImage _fadeRawImage
[SerializeField]
Definition FadeEffect.cs:17
void Fade(FadeAction fadeAction, Action callback, Action centerCallback=null)
Definition FadeEffect.cs:19
IEnumerator FadeInAndOut(Action callback, Action centerCallback)
Definition FadeEffect.cs:60
IEnumerator FadeOut(Action callback)
Definition FadeEffect.cs:49