Unity異步加載場景loading條


using UnityEngine;
using System.Collections;

public class LoadingScene : MonoBehaviour {

    public UISlider processBar;
    private AsyncOperation async;
    private uint _nowprocess;
    // Use this for initialization
    void Start () 
    {
        _nowprocess = 0;
        StartCoroutine(loadScene());
    }

    IEnumerator loadScene()
    {
        //異步讀取場景。
        //Globe.loadName 就是A場景中需要讀取的C場景名稱。
        async = Application.LoadLevelAsync(GlobalVaule.LoadLevelName);
        async.allowSceneActivation = false;
         //讀取完畢后返回, 系統會自動進入C場景
         yield return async;
        
    }

    void Update()
    {
        if(async == null)
        {
            return;
        }

        uint toProcess;
        Debug.Log(async.progress * 100);
        if(async.progress < 0.9f)//坑爹的progress,最多到0.9f
        {
              toProcess = (uint)(async.progress * 100);
        }
        else
        {
             toProcess = 100;
        }

        if(_nowprocess < toProcess)
        {
            _nowprocess++;
        }

        processBar.value = _nowprocess/100f;

        if (_nowprocess == 100)//async.isDone應該是在場景被激活時才為true
        {
            async.allowSceneActivation = true;
        }
    }

}

比較簡單,直接上代碼了,比較坑爹的地方做了注釋~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM