0416 페이드 인으로 씬 전환

2021. 4. 16. 18:23unity

SceneManager가 제공하는 LoadScene 메서드가 있으나, 
해당 장면의 모든 정보를 메모리로 가져오기 전까진 다른 작업 불가=렉 걸림

AsyncOperation 비동기적인 연산을 위한 코루틴

 

using UnityEngine;
using UnityEngine.SceneManagement;

public class App : MonoBehaviour
{
    public enum eSceneType 
    {
        App,Logo
    }
    public eSceneType sceneType;
    // Start is called before the first frame update
    void Start()
    {
        DontDestroyOnLoad(this.gameObject);
        this.ChangeScene(eSceneType.Logo);
    }

    public void ChangeScene(eSceneType sceneType)
    {
        switch (sceneType)
        {
            case eSceneType.Logo:
                {
                    AsyncOperation ao = SceneManager.LoadSceneAsync("Logo");
                    ao.completed += (obj) => {
                        Debug.Log("complete");
                        //씬이 로드완료됨 
                        var logo = GameObject.FindObjectOfType<Logo>();
                        logo.Init();
                    };
                    break;
                }
        }
    }
}
using UnityEngine;
using UnityEngine.UI;

public class Logo : MonoBehaviour
{

    public Image fadeImage;

    public void Init() 
    {
        StartCoroutine(WaitForDisplayLogo());
    }

    private IEnumerator WaitForDisplayLogo() 
    {
        Debug.Log("complete");

        Color startColor = fadeImage.color;
        for (int i = 0; i < 100; i++)
        {
            startColor.a = startColor.a + 0.01f;
            fadeImage.color = startColor;
            yield return new WaitForSeconds(0.01f);
        }
    }
}

 

 

'unity' 카테고리의 다른 글

0420 UI Inventory  (0) 2021.04.20
0419 UI  (0) 2021.04.19
0416 Saving Data in Unity  (0) 2021.04.16
0416 랜덤 아이템 스폰너  (0) 2021.04.16
0416 포스트 프로세싱/렌더링 파이프라인  (0) 2021.04.16