Unity loads a scene asynchronously

Properties(AsyncOperation)

method name

Introduction

allowSceneActivation Automatically load the next scene
isDone Whether the scene is loaded
progress

The value is from 0 to 1 and shows the progress of loading the scene.

If allowSceneActivation is false, up to 0.9

  • Ideas (requires coroutine)
  • Create a variable of the AsyncOperation class to obtain the next scene
  • Set allowSceneActivation to false
  • Detect isDone in every frame
  • Before loading, assign the value of progress to the value of slider. Because the value of progress is 0-1, it can be assigned directly to value.
  • To display the percentage, simply set the value of progress * 100
  • Determine whether the loading is completed. If the progress is 0.9, it means the loading is completed. Because we set allowSceneActivation to false above, it will not jump automatically. You need to set the value to true to jump.
     


        /// <summary>
        /// 异步加载场景的实现
        /// </summary>
        /// <returns></returns>
        protected virtual IEnumerator AsyncLoad(bool loadPanel)
        {
            AsyncOperation operation;

            //获取异步A程序走完 要进入的下个场景B.获取异步走完B场景      A(异步)-->B(场景)
            operation = SceneManager.LoadSceneAsync(sceneName);

            //判断是否开启异步
            if (loadPanel)
            {

                //查找加载异步A的对象
                GameObject panel = GameObject.Find(LoadPanelName);
                if (panel == null)
                {
                    Debug.LogError($"{LoadPanelName}面板不存在");
                    yield break;
                }
                //开启面板进度条
                panel.transform.PanelAppearance(true);
                //  设置下一个场景为false
                operation.allowSceneActivation = false;
                //获取面板面板进度条
                Slider slider = panel.GetComponentInChildren<Slider>();
                //进度条起始值为0
                slider.value = 0;
                float progressValue;

                //	是否加载好场景  true加载好  false未加载好(让走循环)
                while (!operation.isDone)
                {
                    //加载场景进度小于1把当前值赋值。 
                    if (operation.progress < 0.9f)
                        progressValue = operation.progress;
                    else
                        //加载场景进度 >1加载完成  赋值为1
                        progressValue = 1.0f;

                    //赋值给进度条
                    slider.value = progressValue;
                    //进度值大于1
                    if (progressValue >= 0.9f)
                    {   
                        slider.value = 1f;
                        //设置下一个场景为true
                        operation.allowSceneActivation = true;
                    }
                    yield return null;
                }
                //把当前进度条面板进行隐藏
                panel.transform.PanelAppearance(false);
            }
            else
                operation.allowSceneActivation = true;
        }
    }



  /// <summary>
        /// 控制一个面板的外观
        /// </summary>
        /// <param name="t"></param>
        /// <param name="on_off">显示则为true</param>
        /// <param name="active">是否为活动对象</param>
        public static void PanelAppearance(this Transform t, bool on_off, bool active = false)
        {
            CanvasGroup group = t.GetOrAddComponent<CanvasGroup>();
            int value = on_off == true ? 1 : 0;

            //射线检测
            group.blocksRaycasts = on_off;
            //交互
            group.interactable = on_off;
            //透明度
            group.alpha = value;

            t.gameObject.SetActive(on_off || active);
        }
    }

Guess you like

Origin blog.csdn.net/qq_38092788/article/details/132188417