引入命名空間
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; //引入命名空間
using UnityEngine.SceneManagement;//引入命名空間
public class S2Manager : MonoBehaviour { //UI進度條
private Slider _proSlider; //滑動條 //目的是對場景進行控制 獲取進度值 和允許顯示
private AsyncOperation _async; //UI應該達到的進度
private int _currProgress; //1. 獲取滑動條 //協同加載(異步加載 不斷獲取進度值 經過計算賦值給滑動條) // Use this for initialization
void Start () { _currProgress = 0; _async = null; _proSlider = GameObject.Find("Slider").GetComponent<Slider>(); StartCoroutine("LoadScene"); } // Update is called once per frame
void Update () { //目的就是現實進度
_proSlider.value = _currProgress / 100.0f; } IEnumerator LoadScene() { //臨時的進度
int tmp; //異步加載
_async = SceneManager.LoadSceneAsync("S3"); //跳轉場景為S3 //先不顯示場景 等到進度為100%的時候顯示場景 必須的!!!! _async.allowSceneActivation = false; #region 優化進度的
while (_async.progress < 0.9f) { //相當於滑動條應該到的位置
tmp = (int) _async.progress * 100; //當滑動條 < tmp 就意味着滑動條應該變化
while (_currProgress < tmp) { ++_currProgress; yield return new WaitForEndOfFrame(); } }//while end 進度為90%
tmp = 100; while (_currProgress < tmp) { ++_currProgress; yield return new WaitForEndOfFrame(); } #endregion
//處理進度為0 ~0.9的0 //進度條完成 允許顯示
_async.allowSceneActivation = true; } }
同步和異步:
1. 同步直接懟過來 (若機器low或場景大 就會卡)
2. 異步 直接懟到一個中間場景(過度場景(顯示進度條)) --> 到場景S3
3. 在異步中的兩個while循環沒啥大作用, 目的就是優化進度條的!!!