본문 바로가기

Unity

[Unity] Video Player 페이드 인, 페이드 아웃 효과 만들기 (2)

728x90

(1)에서 페이드 인, 페이드 아웃을 구현하였으니 이번에는 적용을 시켜보겠습니다.

 

1
2
3
4
5
6
7
8
9
10
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class PageManager : MonoBehaviour
{
    int number;
    AsyncOperation asyncOperation;

 

number - 씬 번호를 저장할 변수입니다.

asyncOperation - 비동기 장면 전환을 위한 변수입니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    void Awake()
    {
        number = 0;
    }
 
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            NextScene();
        }
    }
 
    void NextScene()
    {
        number++;
        StartCoroutine(TransitionScene());
    }

 

NextScene이 호출되면 TransitionScene 코루틴을 시작해줍니다.

 

1
2
3
4
5
6
    IEnumerator TransitionScene()
    {
        StartCoroutine(VideoManager.instance.FadeOut());
        yield return new WaitUntil(() => VideoManager.instance.isChanged);
        StartCoroutine(AwaitLoadScene());
    }

 

먼저 저번에 구현해놓았던 FadeOut 코루틴을 시작해주고, VideoManager의 isChange 변수가 true가 될 때까지 기다려줍니다. isChanged가 true가 되었다면 AwaitLoadScene 코루틴을 시작해줍니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    IEnumerator AwaitLoadScene()
    {
        asyncOperation = SceneManager.LoadSceneAsync(number);
        asyncOperation.allowSceneActivation = false;
     
        while (!asyncOperation.isDone)
        {
            if (asyncOperation.progress >= 0.9f)
            {
                asyncOperation.allowSceneActivation = true;
                VideoManager.instance.SetVideo();
                StartCoroutine(VideoManager.instance.FadeIn());
            }
            yield return null;
        }
    }
}

 

LoadSceneAsync를 통해서 number에 맞는 씬을 재생시켜줍니다. allowSceneActivation의 값을 false로 바꿔주어 어떠한 입력도 받지 못하게 해 줍니다. 그리고 다음 씬이 준비되기 전까지 isDone이 true가 되기 전까지 while문을 반복해줍니다. 반복 중에 진행이 90% 이상 완료가 되면 allowSceneActivation값을 true로 바꿔주고, setVideo 함수를 실행시켜주고, FadeIn코루틴을 시작해줍니다.

 

 

결과 화면

728x90