[英]Load Scene Unity
I use the function Application.LoadLevelAsync (s);我使用函数 Application.LoadLevelAsync(s); in my game but it just stops at 89%.在我的游戏中,但它只是停在 89% 处。 Help me.帮我。 It's my code:这是我的代码:
public void LoadScenes (string s)
{
if (!quit) {
SoundController.PlaySound (soundGame.ButtomClick);
progressBars.gameObject.SetActive (true);
background.SetActive (true);
text.gameObject.SetActive (true);
async = Application.LoadLevelAsync (s);
async.allowSceneActivation = false;
StartCoroutine (DisplayLoadingScreen ());
}
}
IEnumerator DisplayLoadingScreen ()
{
while (!async.isDone) {
loadProgress = (int)(async.progress * 100);
text.text = "Loading Progress " + loadProgress + "%";
progressBars.size = loadProgress;
yield return null;
}
if (async.isDone) {
Debug.Log ("Loading complete");
async.allowSceneActivation = true;
}
}
When you set async.allowSceneActivation = false
, the loading stops at 90% or 0.9.当您设置async.allowSceneActivation = false
,加载会在 90% 或 0.9 处停止。 This is because unity saves the last 10% to actually show you the scene that has been loaded.这是因为 unity 保存了最后 10% 来实际显示已加载的场景。 So 0 to 0.9 is when the actual loading process takes place and the last .1% is for displaying the loaded scene.所以 0 到 0.9 是实际加载过程发生的时间,最后 0.1% 用于显示加载的场景。
So in your case, you are checking for async.isDone
which would become true only when progress is 100% but you have also set async.allowSceneActivation = false
which would stop the progress at 90%.因此,在您的情况下,您正在检查async.isDone
仅当进度为 100% 时才会变为真,但您还设置了async.allowSceneActivation = false
这将使进度停止在 90%。 What you might want to check is async.progress >=0.9f
and then set your async.allowSceneActivation = true
.您可能想要检查的是async.progress >=0.9f
然后设置您的async.allowSceneActivation = true
。
Reference: https://docs.unity3d.com/ScriptReference/AsyncOperation-progress.html参考: https : //docs.unity3d.com/ScriptReference/AsyncOperation-progress.html
From my experience, AsyncOperation.progress
can cap out at about 89% as you've discovered.根据我的经验,正如您发现的那样, AsyncOperation.progress
可以达到大约 89%。 It may look like it's stalled at that message, when really it's still loading for however long it needs.它可能看起来像是在该消息上停滞不前,但实际上它仍在加载所需的时间。 It won't ever reach 100% even when it's done though, so it's rather useless if you want to implement a loading bar.即使完成了它也永远不会达到 100%,所以如果你想实现一个加载栏,它是没有用的。
As such, async.isDone
might never become true
.因此, async.isDone
可能永远不会成为true
。
As a workaround, you could set async.allowSceneActivation = true;
作为解决方法,您可以设置async.allowSceneActivation = true;
sooner than later, ignoring async.progress
altogether:迟早会完全忽略async.progress
:
async = Application.LoadLevelAsync (s);
async.allowSceneActivation = true;
I tend to use a progress-agnostic rotating loading icon instead of a loading bar because of this issue.由于这个问题,我倾向于使用与进度无关的旋转加载图标而不是加载栏。
On a side note that I haven't tested, others have said that this is an Editor-only issue;顺便提一下,我还没有测试过,其他人说这是一个仅限编辑器的问题; async.progress
and async.isDone
might actually work on standalone builds. async.progress
和async.isDone
实际上可能适用于独立构建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.