unity 异步加载场景并显示进度

在游戏设计中尽量避免使用resource.load这个方法,不然就是使用异步加载场景也会根据手机的配置,场景跳转的慢,尽量使用assert方法。 

private IEnumerator StartLoading(int scene)

    {
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation op = SceneManager.LoadSceneAsync(scene);
        //allowSceneActivation 来控制异步加载的场景暂时不进入。播放完动画后通过触发
        op.allowSceneActivation = false;
        while (op.progress < 0.9f)
        {
            toProgress = (int)op.progress * 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                yield return new WaitForEndOfFrame();
            }
        }
        toProgress = 100;
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            SetLoadingPercentage(displayProgress);
            yield return new WaitForEndOfFrame();
        }
        op.allowSceneActivation = true;
    }
    private void SetLoadingPercentage(float value)
    {
        this.Sld_Progress.GetComponent<Slider>().value = value;
        this.Text_Progress.GetComponent<Text>().text = value.ToString()+"%";
    }

猜你喜欢

转载自blog.csdn.net/qq_33515628/article/details/79239129