아카이브

페이드인, 페이드 아웃 연습 본문

유니티 기초

페이드인, 페이드 아웃 연습

timbercat 2023. 8. 14. 04:20

페이드 아웃 만들기

페이드 아웃 씬과 스크립트 생성
canvas 오브젝트 생성 후 UI 스케일 모드를 Screen 사이즈로 맞추고 1920X1080 사이즈로 변경
canvas 안에 dim(Image)오브젝트 생성 후 1920X1080 사이즈와 알파값(색 선택하는 곳 RGB 밑에있는거)을 0으로 맞춰준다

스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test_FadeOutMain : MonoBehaviour
{
    [SerializeField]
    private Image dim;
    // Start is called before the first frame update
    void Start()
    {
        this.StartCoroutine(this.FadeOut());
    }

    private IEnumerator FadeOut()
    {
        Color color = this.dim.color;
        //점점 어두워지게
        while (true)
        {
            color.a += 0.01f;
            this.dim.color = color;

            Debug.Log(this.dim.color.a);

            if (this.dim.color.a >= 1)
            {
                break;
            }

            yield return null; //다음 프레임 시작
        }
        Debug.LogFormat("fadeout complete!");
    }

Test_FadeOut 오브젝트에 메인 스크립트 적용, Dim에 이미지 오브젝트 적용

페이드인 만들기

페이드 아웃 만들 때와 비슷하지만 dim 의 알파값을 0이 아니라 최대로 맞춰야함

스크립트

FadeOut

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Test_FadeOutMain : MonoBehaviour
{
    [SerializeField]
    private Image dim;
    private System.Action onFadeOutComplete;
    // Start is called before the first frame update
    void Start()
    {
        //이벤트 추가 
        this.onFadeOutComplete = () => {
            //씬전환 
            SceneManager.LoadScene("Test_FadeIn");
        };
        this.StartCoroutine(this.FadeOut());
    }

    private IEnumerator FadeOut()
    {
        Color color = this.dim.color;
        //점점 어두워지게
        while (true)
        {
            color.a += 0.01f;
            this.dim.color = color;

            Debug.Log(this.dim.color.a);

            if (this.dim.color.a >= 1)
            {
                break;
            }

            yield return null; //다음 프레임 시작
        }
        Debug.LogFormat("fadeout complete!");
        this.onFadeOutComplete();   //대리자 호출
    }

FadeIn

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Test_FadeInMain : MonoBehaviour
{
    [SerializeField]
    private Image dim;

    private System.Action onFadeInComplete;

    void Start()
    {
        //이벤트 추가 
        this.onFadeInComplete = () => {

            Debug.Log("FadeIn complete!");
        };
        this.StartCoroutine(this.FadeIn());
    }

    private IEnumerator FadeIn()
    {
        Color color = this.dim.color;
        
        while (true)
        {

            color.a -= 0.01f;
            this.dim.color = color;

            Debug.Log(this.dim.color.a);

            if (this.dim.color.a <= 0)
            {
                break;
            }

            yield return null;  
        }

        Debug.LogFormat("fadeout complete!");

        this.onFadeInComplete();   //대리자 호출 
    }
}

빌드 세팅에서 씬추가