gpt4 book ai didi

unity3d - 在 Unity3D 中异步加载场景不起作用

转载 作者:行者123 更新时间:2023-12-04 15:46:36 27 4
gpt4 key购买 nike

因此,我研究了如何在 unity 上异步加载场景。到目前为止,我发现有两种非常相似的方法,并且基于相同的原则。

   StartCoroutiune(loadScene());

private AsyncOperation async;

// ...

IEnumerator loadScene(){
async = SceneManager.LoadAsyncScene("Scene", LoadSceneMode.Single);
async.allowSceneActivation = false;
while(async.progress < 0.9f){
progressText.text = async.progress+"";
}
while(!async.isDone){
yield return null;
}

}

public void showScene(){
async.allowSceneActivation = true;
}

但是这似乎对我不起作用。即使我没有调用代码来显示它,我仍然需要大量的加载时间并且场景会立即显示。我也试过做

SceneManager.LoadAsyncScene("Scene", LoadSceneMode.Additive);

这是我负责完成这项工作的类(class)。如果我的错误太简单,请原谅我,我是 Unity 的新手。谢谢。

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager_StartGame : MonoBehaviour
{
private GameManager_MasterMenu gameManagerRef;
private AsyncOperation loadGame;

private void OnEnable()
{
SetInitialReferences();
StartCoroutine("loadMainScene");//loads scene before the game starts
gameManagerRef.ContinueGameEvent += StartGame; //subscribing the StartGame method to an Event
}

private void OnDisable()
{
gameManagerRef.ContinueGameEvent -= StartGame;//getting the references to the game Manager
}

void SetInitialReferences()
{
gameManagerRef = GetComponent<GameManager_MasterMenu>();
}

IEnumerator loadMainScene()
{
Debug.LogWarning("ASYNC LOAD STARTED - " +
"DO NOT EXIT PLAY MODE UNTIL SCENE LOADS... UNITY WILL CRASH");
loadGame = SceneManager.LoadSceneAsync(1,LoadSceneMode.Single);
loadGame.allowSceneActivation = false;//setting the allowscene to false so that it won't show it immediately
yield return loadGame;
}


void StartGame()
{
if (GameReferences.currentSave == null)
{
GameReferences.currentSave = GameReferences.dBConnector.GetLastSave();
}
loadGame.allowSceneActivation = true; //is activated from the outside
}
}

最佳答案

由于 while 循环的进度,加载时永远不会触发 yield。尝试将其放入异步 while 循环中,例如

IEnumerator loadScene(){
async = SceneManager.LoadAsyncScene("Scene", LoadSceneMode.Single);
async.allowSceneActivation = false;

while(!async.isDone){
progressText.text = async.progress+"";
yield return null;
}

}

关于unity3d - 在 Unity3D 中异步加载场景不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55486612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com