-
Notifications
You must be signed in to change notification settings - Fork 5
/
FadeInOut.cs
57 lines (48 loc) · 1.4 KB
/
FadeInOut.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class FadeInOut : MonoBehaviour
{
private enum Fade { In, Out }
[SerializeField] private Fade _FadeOption = Fade.In;
[SerializeField] private float _Duration = 0;
[SerializeField] private Image _Image = null;
private float _ChangeSpeed;
private Color _Color;
void Start()
{
if (_Image == null)
_Image = GetComponent<Image>();
if (_FadeOption == Fade.In)
_Color = new Color(_Image.color.r, _Image.color.g, _Image.color.b, 0);
else
_Color = new Color(_Image.color.r, _Image.color.g, _Image.color.b, 1);
_ChangeSpeed = 1 / _Duration;
}
void Update()
{
if (_FadeOption == Fade.In && _Color.a < 1)
{
_Color.a += _ChangeSpeed * Time.deltaTime;
}
if (_FadeOption == Fade.Out && _Color.a > 0)
{
_Color.a -= _ChangeSpeed * Time.deltaTime;
}
_Image.color = _Color;
}
public void SetFade(bool isfadein)
{
if (isfadein)
{
_FadeOption = Fade.In;
_Color = new Color(_Image.color.r, _Image.color.g, _Image.color.b, 0);
}
else
{
_FadeOption = Fade.Out;
_Color = new Color(_Image.color.r, _Image.color.g, _Image.color.b, 1);
}
}
}