關於Unity遊戲異步加載的進度值同步到遊戲中的Slider兩種方式

第一種方式:
在場景中創建一個C#的腳本,名字爲LoadGame,並複製以下的代碼!!!

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadGame : MonoBehaviour {

	//對應的是場景中的滑動條Slider
    public Slider processView;

	void Start () {
        LoadGameMethod();
        
	}
	
    public void LoadGameMethod()
    {
        StartCoroutine(StartLoading_4(2));
    }


	//定義一個迭代器,每一幀返回一次當前的載入進度,同時關閉自動的場景跳轉
	//因爲LoadScenceAsync每幀加載一部分遊戲資源,每次返回一個有跨越幅度的progress進度值
	//當遊戲資源加載完畢後,LoadScenceAsync會自動跳轉場景,所以並不會顯示進度條達到了100%
	//關閉自動場景跳轉後,LoadSceneAsync只能加載90%的場景資源,剩下的10%場景資源要在開啓自動場景跳轉後才加載
	private IEnumerator StartLoading_4(int scene)
    {
        int displayProgress = 0;  //顯示進度
        int toProgress = 0;   //真正的進度
		//異步加載場景
		AsyncOperation op = SceneManager.LoadSceneAsync(scene);
		//阻止當加載完成自動切換       如果加載完成,也不進入場景
		op.allowSceneActivation = false;
		//op.progress最大值爲0.9
		while (op.progress < 0.9f)
        {
            toProgress = (int)op.progress * 100;
			//當真實的加載進度大於顯示進度的話
            while (displayProgress < toProgress)
            {
				//顯示進度自增
                ++displayProgress;
				//把自增過後的進度值通過SetLoadingPercentage方法傳到遊戲中的滑動條的value值
				SetLoadingPercentage(displayProgress);
				//等待一幀 在結束當前幀 攝像機和GUI被渲染以及其它函數完成後纔會繼續執行當行yield return 後面的代碼
				yield return new WaitForEndOfFrame();
            }
        }

		//關閉自動場景跳轉後,LoadSceneAsync只能加載90%的場景資源,剩下的10%場景資源要在開啓自動場景跳轉後才加載
		toProgress = 100;
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            SetLoadingPercentage(displayProgress);
            yield return new WaitForEndOfFrame();
        }

		//允許異步加載完畢後自動切換場景
		op.allowSceneActivation = true;
    }

    private void SetLoadingPercentage(float v)
    {
		//把異步加載的進度值同步到遊戲場景中的slider中的value
        processView.value = v / 100;
    }
}

第二種方式:

使用這種方式同樣需要在場景中新建一個C#的腳本,名字爲LoadingScene,並複製一下的代碼!!!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
 
public class LoadingScene : MonoBehaviour
{
 
	//對應的是場景中的滑動條Slider
	public Slider processBar;
 
	// Application.LoadLevelAsync()這個方法的返回值類型是AsyncOperation
	private AsyncOperation async;
 
	// 當前進度,控制滑動條的百分比
	private uint nowprocess = 0;
 
 
	void Start ()
	{
		// 開啓一個協程
		StartCoroutine (loadScene ());
	}
 
	// 定義一個協程
	IEnumerator loadScene ()
	{
		// 異步讀取場景
		// 指定需要加載的場景名
		async = Application.LoadLevelAsync ("需要加載的場景名字或者index");
 
		// 設置加載完成後不能自動跳轉場景
		async.allowSceneActivation = false;
 
		// 下載完成後返回async
		yield return async;
		
	}
 
	void Update ()
	{
		// 判斷是否加載完需要跳轉的場景數據
		if (async == null) {
			// 如果沒加載完,就跳出update方法,不繼續執行return下面的代碼
			return;
		}
 
		// 進度條需要到達的進度值
		uint toProcess;
		Debug.Log (async.progress * 100);
 
		// async.progress 你正在讀取的場景的進度值  0---0.9
		// 如果當前的進度小於0.9,說明它還沒有加載完成,就說明進度條還需要移動
		// 如果,場景的數據加載完畢,async.progress 的值就會等於0.9
		if (async.progress < 0.9f) {
			//  進度值
			toProcess = (uint)(async.progress * 100);
		}
		// 如果能執行到這個else,說明已經加載完畢
		else {
			// 手動設置進度值爲100
			toProcess = 100;
		}
 
		// 如果滑動條的當前進度,小於,當前加載場景的方法返回的進度
		if (nowprocess < toProcess) {
			// 當前滑動條的進度加一
			nowprocess++;
		}
 
		// 設置滑動條的value
		processBar.value = nowprocess / 100f;
 
		// 如果滑動條的值等於100,說明加載完畢
		if (nowprocess == 100) {
			// 設置爲true的時候,如果場景數據加載完畢,就可以自動跳轉場景
			async.allowSceneActivation = true;
		}
	}
	
} 

好了,通過以上兩種方式的任意一種方式就可以使場景中的Slider滑動條可以流暢的進行加載!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章