Unity - 加载栏经常直接从 0% 到 100%,同时加载 (C#)

Enr*_*vis 5 c# unity-game-engine

我在这里按照Brackeys的教程制作了一个加载栏(后来做了一些更改)。我注意到当我加载一个关卡并且我的加载条出来时,它首先显示0%,等待,然后直接显示100% -没有中间值!。

我猜这可能是由于游戏已经使用了大量硬件而发生的一些延迟,并且没有为其余进程留下足够的电量,因为我尝试在做其他事情的同时加载它同时。

这是加载关卡更改滑块值的代码

    public void LoadLevel(int sceneIndex) {
        StartCoroutine(StartFadeThenLoadLevel(sceneIndex));
    }

    IEnumerator StartFadeThenLoadLevel(int sceneIndex)
    {
        yield return new WaitForSeconds(0.9f);
        StartCoroutine(LoadAsynchronously(sceneIndex));
    }

    IEnumerator LoadAsynchronously(int sceneIndex) {
        yield return null;

        AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);

        loadingScreen.SetActive(true);
        chooseLevel.SetActive(false);

        while (!operation.isDone) {
            float progress = Mathf.Clamp01(operation.progress / 0.12f);

            if (progress > 1) progress = 1;

            slider.value = progress;
            progressText.text = (int)(progress * 100f) + "%";

            yield return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我看到这大多发生在更强大的手机,当它不花费超过第二加载场景,比旧的(慢)的人

我怎样才能解决这个问题?我想要一个非常流畅的加载栏,至少每 5% 更新一次!

在此先感谢您的帮助!

编辑:我添加了一个 FPS 计数器(Graphy),我看到有一个巨大的帧下降到 1fps。这大概就是一切的原因吧!有什么办法可以防止吗?(如果您认为这可能是问题所在,请回答这个问题而不是主要问题!)

Vin*_*llo 3

int progress当 时 为 0 operation.progress <= 0,当 时为 1 operation.progress > 0。所以你的progress值总是 1,因为operation.progress它总是正数。尝试简化您的进度计算:

IEnumerator load(){
    yield return null;

    //Begin to load the Scene you specify
    AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneIndex);
    //Don't let the Scene activate until you allow it to
    asyncOperation.allowSceneActivation = false;
    Debug.Log("Pro :" + asyncOperation.progress);
    //When the load is still in progress, output the Text and progress bar
    while (!asyncOperation.isDone)
    {
        //Output the current progress
        Debug.Log("Loading progress: " + (asyncOperation.progress * 100) + "%");

        // Check if the load has finished
        if (asyncOperation.progress >= 0.9f)
        {
            //Change the Text to show the Scene is ready
            asyncOperation.allowSceneActivation = true;
        }

        yield return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述