|
5 | 5 |
|
6 | 6 | public class FadeManager : MonoBehaviour
|
7 | 7 | {
|
8 |
| - [Header("Fade Event")] |
9 | 8 | [SerializeField] private FadeChannelSO _fadeChannelSO;
|
10 |
| - |
11 |
| - [Header("Fade Image")] |
12 | 9 | [SerializeField] private Image _imageComponent;
|
13 | 10 |
|
14 |
| - private bool IsCurrentlyFading = false; |
| 11 | + private bool _isCurrentlyFading = false; |
15 | 12 |
|
16 |
| - /// <summary> |
17 |
| - /// Enumerators that fades in the canvas's imageComponent to turn the screen to a flat color over time. Fadeins called simeutaneously will only fade in the earliest call and discard any others. |
18 |
| - /// </summary> |
19 |
| - /// <param name="duration">How long it takes to the image to fade in.</param> |
20 |
| - /// <param name="color">Target color for the image to reach.</param> |
21 |
| - /// <returns></returns> |
22 |
| - private IEnumerator FadeInEnum(float duration, Color color) |
| 13 | + private void OnEnable() |
23 | 14 | {
|
24 |
| - float totalTime = 0f; // Total amount of time this coroutine has taken. Determines when the fadein will end and what color the imageComponent should be at every frame. |
25 |
| - while (totalTime <= duration) |
26 |
| - { |
27 |
| - totalTime += Time.deltaTime; |
28 |
| - _imageComponent.color = Color.Lerp(new Color(0, 0, 0, 0), color, totalTime/duration); // Sets the image's color to a mixture between total transparency and the target color, and interpolates based on the amount of time to completion. |
29 |
| - yield return null; |
30 |
| - } |
31 |
| - _imageComponent.color = color; // Here to guarentee the image is exactly the requested color at the end of the loop. |
32 |
| - IsCurrentlyFading = false; |
33 |
| - yield return null; |
| 15 | + _fadeChannelSO.OnEventRaised += InitiateFade; |
| 16 | + } |
| 17 | + |
| 18 | + private void OnDisable() |
| 19 | + { |
| 20 | + _fadeChannelSO.OnEventRaised -= InitiateFade; |
34 | 21 | }
|
35 | 22 |
|
36 | 23 | /// <summary>
|
37 |
| - /// Enumerators that fades out the canvas's imageComponent to turn the screen to normal gameplay color over time. Fadeouts called simeutaneously will only fade out the earliest call and discard any others. |
| 24 | + /// Enumerator that fades in the canvas's imageComponent to turn the screen to a flat color over time. Fadeins called simeutaneously will only fade in the earliest call and discard any others. |
38 | 25 | /// </summary>
|
39 |
| - /// <param name="duration">How long it takes to the image to fade out.</param> |
40 |
| - /// <returns></returns> |
41 |
| - private IEnumerator FadeOutEnum(float duration, Color color = default) |
| 26 | + private IEnumerator FadeCoroutine(bool fadeIn, float duration, Color endColor = default) |
42 | 27 | {
|
43 |
| - if (color == default) |
44 |
| - color = _imageComponent.color; // Stores the old color of the image component, as we can't assume the image will always be black, if no color is specified. |
45 |
| - float totalTime = 0f; // Total amount of time this coroutine has taken. Determines when the fadeout will end and what color the imageComponent should be at every frame. |
| 28 | + Color startColor = _imageComponent.color; |
| 29 | + if (fadeIn) endColor = Color.clear; |
| 30 | + |
| 31 | + float totalTime = 0f; |
| 32 | + |
46 | 33 | while (totalTime <= duration)
|
47 | 34 | {
|
48 | 35 | totalTime += Time.deltaTime;
|
49 |
| - _imageComponent.color = Color.Lerp(color, new Color(0, 0, 0, 0), totalTime / duration); // Sets the image's color to a mixture between the old color and total transparency, and interpolates based on the amount of time to completion. |
50 |
| - yield return null; |
51 |
| - } |
52 |
| - _imageComponent.color = new Color(0, 0, 0, 0); // Here to guarentee the image is fully transparent at the end of the loop. |
53 |
| - IsCurrentlyFading = false; |
54 |
| - yield return null; |
55 |
| - } |
| 36 | + _imageComponent.color = Color.Lerp(startColor, endColor, totalTime/duration); |
56 | 37 |
|
57 |
| - private void OnEnable() |
58 |
| - { |
59 |
| - if (_fadeChannelSO != null) |
60 |
| - { |
61 |
| - _fadeChannelSO.OnEventRaised += fadeGeneral; |
| 38 | + yield return null; |
62 | 39 | }
|
63 |
| - } |
64 | 40 |
|
65 |
| - private void OnDisable() |
66 |
| - { |
67 |
| - if (_fadeChannelSO != null) |
68 |
| - { |
69 |
| - _fadeChannelSO.OnEventRaised += fadeGeneral; |
70 |
| - } |
| 41 | + _imageComponent.color = endColor; //Force to end result |
| 42 | + _isCurrentlyFading = false; |
71 | 43 | }
|
72 | 44 |
|
73 | 45 | /// <summary>
|
74 | 46 | /// Controls the fade-in and fade-out.
|
75 | 47 | /// </summary>
|
76 |
| - /// <param name="fadeIn">If true, the rectangle fades in. If false, the rectangle fades out.</param> |
| 48 | + /// <param name="fadeIn">If true, rectangle fades out and gameplay is visible. If false, the screen becomes black.</param> |
77 | 49 | /// <param name="duration">How long it takes to the image to fade in/out.</param>
|
78 | 50 | /// <param name="color">Target color for the image to reach. Disregarded when fading out.</param>
|
79 |
| - private void fadeGeneral(bool fadeIn, float duration, Color color) |
| 51 | + private void InitiateFade(bool fadeIn, float duration, Color desiredColor) |
80 | 52 | {
|
81 |
| - if (!IsCurrentlyFading) // Makes sure multiple fade-ins or outs don't happen at the same time. Note this will mean fadeouts called at the same time will be discarded. |
| 53 | + if (!_isCurrentlyFading) // Makes sure multiple fade-ins or outs don't happen at the same time. Note this will mean fadeouts called at the same time will be discarded. |
82 | 54 | {
|
83 |
| - IsCurrentlyFading = true; |
84 |
| - if (fadeIn) |
85 |
| - { |
86 |
| - StartCoroutine(FadeInEnum(duration, color)); |
87 |
| - } |
88 |
| - else |
89 |
| - { |
90 |
| - StartCoroutine(FadeOutEnum(duration, color)); // Fadeout doesn't need color, so the color parameter is disregarded. |
91 |
| - } |
| 55 | + _isCurrentlyFading = true; |
| 56 | + StartCoroutine(FadeCoroutine(fadeIn, duration, desiredColor)); |
92 | 57 | }
|
93 | 58 | }
|
94 | 59 | }
|
0 commit comments