1.使用一個棧來儲存玩家在遊戲中情境的載入先後關係(方便Back功能實現以及記錄當前情境ID)
2.提供切換情境,壓棧情境,出棧情境方法
3.提供非同步載入情境,並提供載入進度(用以顯示Loading條)
DoTransition(),代碼如下:
protected virtual IEnumerator DoTransition() {// 第一部分:之前情境退齣動畫state = SMTransitionState.Out;Prepare();float time = 0;while(Process(time)) {time += Time.deltaTime;// wait for the next frameyield return 0;}// wait another frame...yield return 0;// 第二部分:保證SMTransition對象不被銷毀(完成後續動畫)state = SMTransitionState.Hold;DontDestroyOnLoad(gameObject);// wait another frame...yield return 0;IEnumerator loadLevel = DoLoadLevel();while (loadLevel.MoveNext()) {yield return loadLevel.Current;} // wait another frame...yield return 0;// 第三部分:新情境載入動畫state = SMTransitionState.In;Prepare();time = 0;while(Process(time)) {time += Time.deltaTime;// wait for the next frameyield return 0;}// wait another frame...yield return 0;Destroy(gameObject);}
在SMTransition的子類中,分別實現Prepare()虛方法和Process(float elapsedTime)抽象方法
protected override bool Process(float elapsedTime) {float effectTime = elapsedTime;// invert direction if necessaryif (state == SMTransitionState.In) {effectTime = duration - effectTime;}progress = SMTransitionUtils.SmoothProgress(0, duration, effectTime);return elapsedTime < duration;}
public void OnGUI() {GUI.depth = 0;Color c = GUI.color;GUI.color = new Color(1, 1, 1, progress);GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), overlayTexture);GUI.color = c;}
其它SMTransition子類也通過Process(float elapsedTime)實現切換動畫效果!
PS: 在非同步載入情境中,Scene Manager中並沒有提供一個擷取當前載入進度的介面,需要自己實現,在SMTransition類中
protected virtual YieldInstruction LoadLevel() {if (loadAsync) { AsyncOperation ao = Application.LoadLevelAsync(screenId); Debug.Log("Progress: " + ao.progress); return ao; //return Application.LoadLevelAsync(screenId);} else {Application.LoadLevel(screenId);return null;}}