Unity Fade in Fade out 유니티 페이드 인 페이드 아웃 스크립트


싱글톤으로 만들어서 사용.



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 System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class TransitionManager : MonoBehaviour
{
    public Image _Image;
 
    Color blackColor = Color.black;
    Color offColor = Color.clear;
    Color startColor = Color.black;
    Color targetColor = Color.black;
 
    private bool isOnTransition = false;
    float fadeTime = 0.5f;
    float delay = 0f;
    string nextScene = "";
 
    void Start()
    {
        BlackIn(0.5f);
        Debug.Log("fadeIn");
    }
 
    public void BlackIn(float a_fadeTime = 0.5f, float a_delay = 0f)
    {
        fadeTime = a_fadeTime;
        delay = a_delay;
 
        _Image.enabled = true;
        _Image.color= blackColor;
        startColor = blackColor;
        targetColor = offColor;
        _Image.raycastTarget = false;
 
        if (isOnTransition)
            StopCoroutine("UpdateColorCoroutine");
 
        StartCoroutine("UpdateColorCoroutine");
    }
 
    public void BlackOut(float a_fadeTime = 0.5f, float a_delay = 0f, string a_nextScene = "")
    {
        fadeTime = a_fadeTime;
        delay = a_delay;
        nextScene = a_nextScene;
 
        _Image.enabled = true;
        startColor = _Image.color;
        targetColor = blackColor;
        _Image.raycastTarget = true;
 
        if (isOnTransition)
            StopCoroutine("UpdateColorCoroutine");
 
        StartCoroutine("UpdateColorCoroutine");
    }
 
    IEnumerator UpdateColorCoroutine()
    {
        isOnTransition = true;
 
        if(delay != 0)
            yield return new WaitForSeconds(delay);
 
        float t = 0;
        while (t < 1)
        {
            _Image.color = Color.Lerp(startColor, targetColor, t);
            t += Time.deltaTime / fadeTime;
            yield return new WaitForEndOfFrame();
        }
        
        if (targetColor == Color.clear) // 시간을 지났을 경우! 즉 트랜지션 끝남
            _Image.enabled = false;
        else if(nextScene != "")
            LoadingManager.LoadScene(nextScene);
 
        isOnTransition = false;
    }
}
 




댓글

Designed by JB FACTORY