Skip to content

Commit 191b661

Browse files
committed
feat(UI): Add fade in/out on canvas
1 parent 1ab3a7c commit 191b661

File tree

1 file changed

+141
-6
lines changed

1 file changed

+141
-6
lines changed

Assets/JCSUnity/Scripts/UI/JCS_Canvas.cs

Lines changed: 141 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ public class JCS_Canvas : MonoBehaviour
2525

2626
private const string RESIZE_UI_PATH = "UI/ResizeUI";
2727

28-
public Action<JCS_Canvas> onShow = null; // Execution when canvas is shown.
29-
public Action<JCS_Canvas> onHide = null; // Execution when canvas is hidden.
28+
// Execution when canvas is shown.
29+
public Action<JCS_Canvas> onShow = null;
30+
// Execution when canvas is hidden.
31+
public Action<JCS_Canvas> onHide = null;
32+
33+
// Execution when canvas is shown by fading.
34+
public Action<JCS_Canvas> onShowFade = null;
35+
// Execution when canvas is hidden by fading.
36+
public Action<JCS_Canvas> onHideFade = null;
3037

3138
private Canvas mCanvas = null;
3239

40+
private CanvasGroup mCanvasGroup = null;
41+
3342
private RectTransform mAppRect = null; // Application Rect (Window)
3443

3544
[Separator("Check Variables (JCS_Canvas)")]
@@ -51,6 +60,34 @@ public class JCS_Canvas : MonoBehaviour
5160

5261
[Separator("Runtime Variables (JCS_Canvas)")]
5362

63+
[Tooltip("Fade canvas by default.")]
64+
[SerializeField]
65+
private bool mFade = false;
66+
67+
[Tooltip("Fade friction.")]
68+
[SerializeField]
69+
[Range(0.0001f, 30.0f)]
70+
private float mFadeFriction = 0.2f;
71+
72+
[Tooltip("Full fade in amount.")]
73+
[SerializeField]
74+
[Range(0.0f, 1.0f)]
75+
private float mFadeInAmount = 1.0f;
76+
77+
[Tooltip("Full fade out amount.")]
78+
[SerializeField]
79+
[Range(0.0f, 1.0f)]
80+
private float mFadeOutAmount = 0.0f;
81+
82+
// Target fading alpha.
83+
private float mFadeAlpa = 0.0f;
84+
85+
private JCS_FadeType mFading = JCS_FadeType.IN;
86+
87+
[Tooltip("Time type.")]
88+
[SerializeField]
89+
private JCS_TimeType mTimeType = JCS_TimeType.UNSCALED_DELTA_TIME;
90+
5491
[Tooltip("Play sound when active the canvas.")]
5592
[SerializeField]
5693
private AudioClip mActiveSound = null;
@@ -63,11 +100,17 @@ public class JCS_Canvas : MonoBehaviour
63100

64101
public RectTransform AppRect { get { return this.mAppRect; } }
65102
public Canvas canvas { get { return this.mCanvas; } }
103+
public CanvasGroup canvasGroup { get { return this.mCanvasGroup; } }
66104
public JCS_ResizeUI ResizeUI { get { return this.mResizeUI; } }
67105

68106
public bool DisplayOnAwake { get { return this.mDisplayOnAwake; } }
69107
public bool MainCanvas { get { return this.mMainCanvas; } }
70108

109+
public bool Fade { get { return this.mFade; } set { this.mFade = value; } }
110+
public float FadeFriction { get { return this.mFadeFriction; } set { this.mFadeFriction = value; } }
111+
public float FadeInAmount { get { return this.mFadeInAmount; } set { this.mFadeInAmount = value; } }
112+
public float FadeOutAmount { get { return this.mFadeOutAmount; } set { this.mFadeOutAmount = value; } }
113+
public JCS_TimeType TimeType { get { return this.mTimeType; } set { this.mTimeType = value; } }
71114
public AudioClip ActiveSound { get { return this.mActiveSound; } set { this.mActiveSound = value; } }
72115
public AudioClip DeactiveSound { get { return this.mDeactiveSound; } set { this.mDeactiveSound = value; } }
73116

@@ -79,6 +122,7 @@ private void Awake()
79122

80123
this.mAppRect = this.GetComponent<RectTransform>();
81124
this.mCanvas = this.GetComponent<Canvas>();
125+
this.mCanvasGroup = this.GetComponent<CanvasGroup>();
82126

83127
if (JCS_UISettings.instance.RESIZE_UI && !JCS_ScreenSettings.instance.IsNone())
84128
{
@@ -115,6 +159,11 @@ private void Start()
115159
NoMainCanvas();
116160
}
117161

162+
private void Update()
163+
{
164+
DoFading();
165+
}
166+
118167
/// <summary>
119168
/// Return the `canvas` that is the parent of the `trans` object.
120169
///
@@ -166,10 +215,27 @@ public bool IsShown()
166215
/// </summary>
167216
public void Show(bool mute = false)
168217
{
169-
mCanvas.enabled = true;
218+
Show(mFade, mute);
219+
}
220+
public void Show(bool fade, bool mute = false)
221+
{
170222
if (!mute)
171223
JCS_SoundPlayer.PlayByAttachment(mDeactiveSound, JCS_SoundMethod.PLAY_SOUND);
172224

225+
if (fade)
226+
{
227+
mFading = JCS_FadeType.IN;
228+
229+
mFadeAlpa = mFadeInAmount;
230+
}
231+
else
232+
{
233+
mCanvas.enabled = true;
234+
235+
if (mCanvasGroup != null)
236+
mCanvasGroup.alpha = mFadeInAmount;
237+
}
238+
173239
onShow?.Invoke(this);
174240
}
175241

@@ -178,23 +244,45 @@ public void Show(bool mute = false)
178244
/// </summary>
179245
public void Hide(bool mute = false)
180246
{
181-
mCanvas.enabled = false;
247+
Hide(mFade, mute);
248+
}
249+
public void Hide(bool fade, bool mute = false)
250+
{
182251
if (!mute)
183252
JCS_SoundPlayer.PlayByAttachment(mActiveSound, JCS_SoundMethod.PLAY_SOUND);
184253

254+
if (fade)
255+
{
256+
mFading = JCS_FadeType.OUT;
257+
258+
mFadeAlpa = mFadeOutAmount;
259+
}
260+
else
261+
{
262+
mCanvas.enabled = false;
263+
264+
if (mCanvasGroup != null)
265+
mCanvasGroup.alpha = mFadeOutAmount;
266+
}
267+
185268
onHide?.Invoke(this);
186269
}
187270

188271
/// <summary>
189272
/// Toggle the canvas' visibility.
190273
/// </summary>
191274
/// <param name="mute"> True to mute the sound. </param>
275+
///
192276
public void ToggleVisibility(bool mute = false)
277+
{
278+
ToggleVisibility(mFade, mute);
279+
}
280+
public void ToggleVisibility(bool fade, bool mute = false)
193281
{
194282
if (IsShown())
195-
Hide(mute);
283+
Hide(fade, mute);
196284
else
197-
Show(mute);
285+
Show(fade, mute);
198286
}
199287

200288
/// <summary>
@@ -240,5 +328,52 @@ private void FitScreenSize(RectTransform rect)
240328
newScale.y /= screenRaio.height;
241329
rect.localScale = newScale;
242330
}
331+
332+
/// <summary>
333+
/// Do the fading effect.
334+
/// </summary>
335+
private void DoFading()
336+
{
337+
if (mFading == JCS_FadeType.NONE)
338+
return;
339+
340+
if (mCanvasGroup == null)
341+
{
342+
JCS_Debug.LogReminder($"Fade missing the canvas group: {name}");
343+
return;
344+
}
345+
346+
float direction = mFadeAlpa - mCanvasGroup.alpha;
347+
348+
mCanvasGroup.alpha += direction / mFadeFriction * JCS_Time.ItTime(mTimeType);
349+
350+
float diff = Mathf.Abs(direction);
351+
352+
// When close enough.
353+
if (diff < JCS_Constants.NEAR_THRESHOLD)
354+
{
355+
switch (mFading)
356+
{
357+
case JCS_FadeType.IN:
358+
{
359+
mCanvasGroup.alpha = mFadeInAmount;
360+
361+
onShowFade?.Invoke(this);
362+
}
363+
break;
364+
case JCS_FadeType.OUT:
365+
{
366+
mCanvasGroup.alpha = mFadeOutAmount;
367+
368+
onHideFade?.Invoke(this);
369+
}
370+
break;
371+
}
372+
373+
mFadeAlpa = mCanvasGroup.alpha;
374+
375+
mFading = JCS_FadeType.NONE;
376+
}
377+
}
243378
}
244379
}

0 commit comments

Comments
 (0)