Ejemplo n.º 1
0
        /// <summary>
        /// 加载场景
        /// </summary>
        /// <param name="t_SceneName"></param>
        public void LoadScene(string t_SceneName, LoadSceneModle t_LoadSceneModle = LoadSceneModle.Append, bool t_AllowSceneActivation = false)
        {
            UnityEngine.SceneManagement.Scene t_TargetScene = UnityEngine.SceneManagement.SceneManager.GetSceneByName(t_SceneName);

            if (!t_TargetScene.isLoaded)
            {
                StartCoroutine(LoadSceneAsync(t_SceneName, t_TargetScene, t_LoadSceneModle, t_AllowSceneActivation));
            }
            else
            {
                if (null != LoadFailureEventHandler)
                {
                    LoadFailureEventHandler(this, new SceneEventArgs()
                    {
                        SceneName = t_SceneName, Process = 0f, Scene = t_TargetScene
                    });
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 加载场景
 /// </summary>
 /// <param name="t_SceneName"></param>
 public void LoadScene(string t_SceneName, LoadSceneModle t_LoadSceneModle = LoadSceneModle.Append, bool t_AllowSceneActivation = false)
 {
     TargetSceneName = t_SceneName;
     m_SceneManager.LoadScene(t_SceneName, t_LoadSceneModle, t_AllowSceneActivation);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 协程加载场景
        /// </summary>
        /// <param name="t_SceneName"></param>
        /// <returns></returns>
        private IEnumerator LoadSceneAsync(string t_SceneName, UnityEngine.SceneManagement.Scene t_TargetScene, LoadSceneModle t_LoadSceneModle = LoadSceneModle.Append, bool t_AllowSceneActivation = false)
        {
            AsyncOperation t_Async = null;

            if (t_LoadSceneModle == LoadSceneModle.Append)
            {
                t_Async = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(t_SceneName, UnityEngine.SceneManagement.LoadSceneMode.Additive);
            }
            else
            {
                t_Async = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(t_SceneName, UnityEngine.SceneManagement.LoadSceneMode.Single);
            }

            float t_Progres = 0;

            yield return(new WaitForEndOfFrame());

            t_Async.allowSceneActivation = t_AllowSceneActivation;

            while (99.9f > t_Progres)
            {
                t_Progres = t_Async.progress * 100f;
                if (89.9f > t_Progres) //场景加载完毕前
                {
                    //t_Progres = Mathf.MoveTowards(t_Progres, t_Async.progress * 100f, Time.deltaTime*1000f);

                    if (null != LoadUpdateEventHandler)
                    {
                        LoadUpdateEventHandler(this, new SceneEventArgs()
                        {
                            SceneName = t_SceneName, Process = t_Progres, Scene = t_TargetScene
                        });
                    }
                }
                else //当场景加载完毕
                {
                    //t_Progres = Mathf.Lerp(t_Progres, 100f, Time.deltaTime);
                    t_Progres = 100f;
                    if (null != LoadUpdateEventHandler)
                    {
                        LoadUpdateEventHandler(this, new SceneEventArgs()
                        {
                            SceneName = t_SceneName, Process = t_Progres, Scene = t_TargetScene
                        });
                    }
                }

                yield return(null);
            }

            if (null != LoadUpdateEventHandler)
            {
                LoadUpdateEventHandler(this, new SceneEventArgs()
                {
                    SceneName = t_SceneName, Process = 100f, Scene = t_TargetScene
                });
            }

            yield return(new WaitForEndOfFrame()); //平滑过渡

            if (null != LoadSuccessEventHandler)
            {
                LoadSuccessEventHandler(this, new SceneEventArgs()
                {
                    SceneName = t_SceneName, Process = t_Progres, Scene = t_TargetScene
                });
            }

            if (!t_AllowSceneActivation)
            {
                t_Async.allowSceneActivation = !t_AllowSceneActivation;
            }

            yield return(t_Async);
        }