1. 程式人生 > >unity非同步載入 平滑載入,賊順溜

unity非同步載入 平滑載入,賊順溜

     在使用unity進行非同步載入操作時,會遇到載入10%左右和 70%左右卡頓等待,,,,抓狂,,通過我的學習和整理

Copy直接用

,寫出如下程式碼:

using UnityEngine;
using System.Collections;


public class LKPYLoading : MonoBehaviour {


    public UIProgressBar londingBar;
    public UILabel loadinglable;
    AsyncOperation asyncc;
    float displayProgress = 0;
    int toProgress = 0;


    // Use this for initialization
    void Start()
    {
        StartCoroutine(loadScene());
    }


    private void SetLoadingPercentage()
    {
        loadinglable.text = "Loading..." + displayProgress + "%";
    }  
    IEnumerator loadScene() {
        asyncc = Application.LoadLevelAsync("GameDSBY");
        asyncc.allowSceneActivation = false;
        while (asyncc.progress<0.9f){
            toProgress = (int)asyncc.progress * 100;
            while (displayProgress < toProgress){
                ++displayProgress;
                SetLoadingPercentage();
                yield return new WaitForEndOfFrame(); 
            }           
        }
        toProgress = 100;
        while (displayProgress <toProgress )
        {
            ++displayProgress;
            SetLoadingPercentage();
            yield return new WaitForEndOfFrame();  
        }
        asyncc.allowSceneActivation = true;
    }


    void Update()
    {


        londingBar.value = displayProgress/100;
        }
    }