本文實例為大家分享了unity實現場景切換進度條顯示的具體代碼,供大家參考,具體內容如下
一、UI。建立slider適當更改即可;


二、新增loadScene腳本,用來進行場景切換,將其綁定任意物體上面。博主以放置主相機為例。參數分別為進度條(用來設置value值),顯示進度文本text;

在設置中加入兩個場景:

三、腳本;
/// <summary>/// 場景切換/// 在unity 獲取當前加載進度progress中,其中最多到0.9.只有等到加載到第二個場景才會到1/// 所有在加載進度條時如果progress的值近似0.9,則直接將進度參數設置為1,實現進度到100%/// 并且progress的值是在一幀加載一些資源,所以其值不會是連續的,因此設置兩個參數來記錄當前/// 進度和頁面顯示的進度,進行++。/// </summary>public class loadScene : MonoBehaviour{ AsyncOperation async; public Slider slider; public Text text;//百分制顯示進度加載情況 void Start() { //開啟協程 StartCoroutine("loginMy"); } void Update() { } IEnumerator loginMy() { int displayProgress = 0; int toProgress = 0; AsyncOperation op = SceneManager.LoadSceneAsync(1); op.allowSceneActivation = false; while (op.progress < 0.9f) //此處如果是 <= 0.9f 則會出現死循環所以必須小0.9 { toProgress = (int)op.progress * 100; while (displayProgress < toProgress) { ++displayProgress; SetLoadingPercentage(displayProgress); yield return new WaitForEndOfFrame();//ui渲染完成之后 } } toProgress = 100; while (displayProgress < toProgress) { ++displayProgress; SetLoadingPercentage(displayProgress); yield return new WaitForEndOfFrame(); } op.allowSceneActivation = true; } private void SetLoadingPercentage(int displayProgress) { slider.value = displayProgress; text.text = displayProgress.ToString() + "%"; }}四、運行:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答