Skip to content

Commit b4af80c

Browse files
committed
Add extension methods for UI canvas
1 parent 16a1cd9 commit b4af80c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using UnityEngine;
2+
using UnityEngine.UI;
3+
4+
namespace Zigurous.UI
5+
{
6+
/// <summary>
7+
/// Extension methods for UI canvas.
8+
/// </summary>
9+
public static class CanvasExtensions
10+
{
11+
/// <summary>
12+
/// Fades the alpha of all graphics of the canvas to 100% over the given
13+
/// <paramref name="duration"/>.
14+
/// </summary>
15+
/// <param name="canvas">The canvas to fade.</param>
16+
/// <param name="duration">The amount of seconds it takes to fade the graphics.</param>
17+
/// <param name="ignoreTimeScale">Ignores the time scale when fading the graphics.</param>
18+
public static void FadeInGraphics(this Canvas canvas, float duration, bool ignoreTimeScale = false)
19+
{
20+
Graphic[] graphics = canvas.GetComponentsInChildren<Graphic>();
21+
22+
for (int i = 0; i < graphics.Length; i++) {
23+
graphics[i].CrossFadeAlpha(1.0f, duration, ignoreTimeScale);
24+
}
25+
}
26+
27+
/// <summary>
28+
/// Fades the alpha of all graphics of the canvas to 0% over the given
29+
/// <paramref name="duration"/>.
30+
/// </summary>
31+
/// <param name="canvas">The canvas to fade.</param>
32+
/// <param name="duration">The amount of seconds it takes to fade the graphics.</param>
33+
/// <param name="ignoreTimeScale">Ignores the time scale when fading the graphics.</param>
34+
public static void FadeOutGraphics(this Canvas canvas, float duration, bool ignoreTimeScale = false)
35+
{
36+
Graphic[] graphics = canvas.GetComponentsInChildren<Graphic>();
37+
38+
for (int i = 0; i < graphics.Length; i++) {
39+
graphics[i].CrossFadeAlpha(0.0f, duration, ignoreTimeScale);
40+
}
41+
}
42+
43+
}
44+
45+
}

Runtime/Extensions/CanvasExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)