씬 gameObject
에서 DontDestroyOnLoad
씬 으로 이동하려면 씬이 완전히로드 될 때까지 기다려야 합니다. 너무 빨리하면 (을 호출 한 직후 SceneManager.LoadScene()
)가 gameObject
사라집니다. 이 게시물을 기반 으로이 문제를 해결하기 위해 장면 로딩 클래스를 구현했습니다.
public static class CustomSceneManager
{
public delegate void SceneChange(string sceneName);
public static event SceneChange LoadScene;
public static event SceneChange UnloadScene;
private static IEnumerator LoadLevel (string sceneName){
var asyncLoadLevel = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
while (!asyncLoadLevel.isDone){
Debug.Log("Loading the Scene");
yield return null;
}
}
public static void OnLoadScene(string newSceneName)
{
OnUnloadScene(newSceneName);
LoadLevel(newSceneName);
LoadScene?.Invoke(newSceneName);
}
private static void OnUnloadScene(string newSceneName)
{
UnloadScene?.Invoke(newSceneName);
}
}
두 개의 이벤트를 호출하고 있습니다 ( LoadScene
및 UnloadScene
). 그러나 LoadLevel(newSceneName)
작동하지 않습니다. 단순히 장면을로드하지 않습니다. 내가 여기서 뭘 잘못하고 있니?
편집하다:
이제 다음과 같이 methid를 MonoBehavior
호출하는 스크립트 의 참조를 전달합니다 OnLoadScene
.
public static void OnLoadScene(MonoBehaviour loader, string newSceneName)
{
UnloadScene?.Invoke(newSceneName);
loader.StartCoroutine(LoadLevel(newSceneName));
Debug.Log(SceneManager.GetActiveScene().name); // this line returns previous scene
LoadScene?.Invoke(newSceneName);
}
이제 장면이로드되지만 현재로드 된 장면을 확인하면 이전 장면 이름을 반환합니다.
편집 2 :
더 정확히 말하면 내가 교체 Debug.Log(SceneManager.GetActiveScene().name);
로 Debug.Log(SceneManager.GetSceneByName(newSceneName).isLoaded);
그것은 반환 False
.
을 사용하여 coroutienes를 실행해야합니다 StartCoroutine
.
MonoBehaviour
코 루틴을 실행할 참조를 전달 하거나 단순히 클래스를 절대 파괴되지 않는 싱글 톤으로 만들어야합니다.
실제로 이벤트가 아직로드되지 않았을 때 이벤트를 너무 일찍 호출하지만 방금로드하기 시작 했으므로 예를 들어
public class CustomSceneManager : MonoBehaviour
{
public delegate void SceneChange(string sceneName);
public static event SceneChange LoadScene;
public static event SceneChange UnloadScene;
private CustomNetworkManager singleton;
private void Awake ()
{
if(singleton && singleton != this)
{
Destroy(gameObject);
}
singleton = this;
DontDestroyOnLoad (gameObject);
}
private static IEnumerator LoadLevel (string sceneName){
var asyncLoadLevel = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
while (!asyncLoadLevel.isDone){
Debug.Log("Loading the Scene");
yield return null;
}
LoadScene?.Invoke(newSceneName);
}
public static void OnLoadScene(string newSceneName)
{
if(! singleton)
{
singleton = new GameObject("CustomNetworkManager").AddComponent<CustomNetworkManager>();
}
OnUnloadScene(newSceneName);
singleton.StartCoroutine(LoadLevel(newSceneName));
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다