我正在制作一个用于加载场景的统一进度条,但进度条从 0 到 100,中间没有停止
public void Loading(string sceneID)
{
screen.SetActive(true);
StartCoroutine(LoadingAsync(sceneID));
}
public IEnumerator LoadingAsync(string sceneID)
{
yield return null;
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneID);
operation.allowSceneActivation = false;
while (!operation.isDone)
{
float progress = Mathf.Clamp01(operation.progress / .9f);
Debug.Log(progress * 100 + "%");
loadingBar.value = progress * 100f;
loadingPer.text = progress * 100f + "%";
if (operation.progress >= .9f) { operation.allowSceneActivation = true; }
yield return null;
}
}
预计加载栏进度会很顺利,但它只是从 0 到 100
使用 Mathf.clamp01 你只有 0 或 1,其中 1 是你的 100%。 更改此代码以获得真正的进度
while (!operation.isDone)
{
float progress = operation.progress * 100);
Debug.Log(progress + "%");
loadingBar.value = progress;
loadingPer.text = progress + "%";
if (operation.progress >= .9f) { operation.allowSceneActivation = true; }
yield return null;
}