加载进度自动从0到100,无需满载场景

问题描述 投票:0回答:1

我正在制作一个用于加载场景的统一进度条,但进度条从 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

c# unity-game-engine
1个回答
0
投票

使用 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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.