Unity/Unity 리서치

Loading Scnene 구현

lipnus 2021. 7. 12. 20:16
반응형

https://docs.unity3d.com/ScriptReference/AsyncOperation-progress.html

 

Unity - Scripting API: AsyncOperation.progress

Return an operation's progress. (Read Only) This returns how close the operation is to finishing. The operation is finished when the progress float reaches 1.0 and isDone is called. If you set allowSceneActivation to false, progress is halted at 0.9 until

docs.unity3d.com

 

 

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

public class LoadingSceneManager : MonoBehaviour
{
    private static string nextScene;
    [SerializeField] private TextMeshProUGUI progressText;

    public static void ChangeScene(string sceneName) {
        SceneManager.LoadSceneAsync("LoadingScene");
        nextScene = sceneName;
    }

    void Start()
    {
        StartCoroutine(loadScene(1f, nextScene));
    }
    
    IEnumerator loadScene(float delayTime, string sceneName)
    { 
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync( sceneName );
        asyncOperation.allowSceneActivation = false;
        progressText.text = "Progress: " + asyncOperation.progress;
        
        yield return new WaitForSeconds(delayTime);
        asyncOperation.allowSceneActivation = true;
    }
}

100%되고 1초 후 다음 씬으로 이동

반응형