Unity3D開發(三):情境管理外掛程式Scene Manager

來源:互聯網
上載者:User

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;}}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.