Fadder.cs
1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using UnityEngine;
namespace LAViD.VLibras.UI {
public class Fadder : ExchangeableVisibility {
public float visibleAlpha = 1f;
public float hiddenAlpha = 0f;
public float showingSpeed = 0.01f;
public float hiddingSpeed = 0.01f;
public bool disableWhenHidden = true;
private CanvasRenderer mainRenderer;
private CanvasRenderer[] renderers;
private bool visibilityChange = false;
private readonly Vector3 visibleScale = new Vector3(1, 1, 1);
private readonly Vector3 hiddenScale = new Vector3(0, 0, 0);
public override void Animate(bool visible)
{
if (base.isVisible() != visible)
{
this.gameObject.transform.localScale = visibleScale;
base.Animate(visible);
this.visibilityChange = true;
}
}
public void brutallySetVisible(bool visible)
{
base.Animate(visible);
float alpha = visible ? visibleAlpha : hiddenAlpha;
foreach (CanvasRenderer renderer in renderers)
renderer.SetAlpha(alpha);
updateScale();
}
private void updateScale()
{
if (disableWhenHidden && this.mainRenderer.GetAlpha() == hiddenAlpha)
this.gameObject.transform.localScale = hiddenScale;
}
void Start()
{
this.mainRenderer = this.gameObject.GetComponent<CanvasRenderer>();
this.renderers = this.gameObject.GetComponentsInChildren<CanvasRenderer>();
this.hiddingSpeed = -hiddingSpeed;
this.brutallySetVisible(base.isVisible());
}
void Update()
{
if (visibilityChange)
{
float objective = base.isVisible() ? this.visibleAlpha : this.hiddenAlpha;
this.visibilityChange = false;
foreach (CanvasRenderer renderer in renderers)
{
float alpha = renderer.GetAlpha();
if (alpha != objective)
{
float speed = base.isVisible() ? this.showingSpeed : this.hiddingSpeed;
renderer.SetAlpha(Mathf.Abs(alpha - objective) < Mathf.Abs(speed) ? objective : alpha + speed);
this.visibilityChange = true;
}
}
if (this.visibilityChange) this.updateScale();
}
}
}
}