大家好,我是橙子,今天為大家?guī)淼氖荱nity場景切換進度條的教程。
★,??★,??★,??★,--------------------華麗的分割線--------------------??★,??★,??★,??★,??
首先問一個問題:為什么要用進度條,以及什么情況下才用進度條呢?
答:有一些場景,里面包含非常多的資源,比如一個很大的游戲場景,往往加載很慢。這個時候,如果你什么都不干,在場景加載的過程中,用戶會認為"游戲卡死"了,所以用一個進度條來過度,增加游戲體驗。 話不多說 ,直接上才藝!
一、 創(chuàng)建游戲場景及搭建面板UI
首先,我們創(chuàng)建一個游戲場景
然后我們新建一個UI->>Panel
再新建一個Skuder(滑動條) 如果有小伙伴不知道Skuder是什么,可以先去看看Unity的UGUI教程再來哦
就像這樣 tips:按鍵盤上的T 然后鼠標選中邊框,按住Ait鍵 可以等比放大哦~
我們可以在這里,修改滑動塊的樣式
我的設(shè)置好了,不知道你們是什么樣的呢?
在這里全選他們,然后設(shè)置一下錨點,可以適應(yīng)不同設(shè)備的分辨率哦~
我的進度條大概就是這樣了。你的做完了嗎?
二 、添加代碼
我們新建一個C#腳本 名為LoadSceneManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LoadSceneManager : MonoBehaviour
{
public GameObject loadScreen;//顯示進度條的面板
public Slider slider;//滑動條組件
public Text text;//顯示百分比的文字
}
定義好以后,咱們回到Unity去賦值。然后添加Button的綁定事件(你切換場景的按鈕)
賦值完成后 我們繼續(xù)寫代碼
(1)加載完成后進入場景完整代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadSceneManager : MonoBehaviour
{
public GameObject loadScreen;//顯示進度條的面板
public Slider slider;//滑動條組件
public Text text;//顯示百分比的文字
//別忘了給按鈕添加點擊事件
public void LoadNextLevel()
{
StartCoroutine(Loadlevel());
}
IEnumerator Loadlevel()
{
loadScreen.SetActive(true);
AsyncOperation operation = SceneManager.LoadSceneAsync("Main");
while (!operation.isDone)//isDone 是否完成進度條
{
slider.value = operation.progress;
text.text = operation.progress * 100 + "%";//百分比
if (operation.progress>=0.9f)//如果進度條已經(jīng)到達90%
{
slider.value = 1; //那就讓進度條的值編變成1
text.text = "加載完成!";
}
yield return null;
}
}
}
(2)加載完成 需要按鍵后 進入場景完整代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadSceneManager : MonoBehaviour
{
public GameObject loadScreen;//顯示進度條的面板
public Slider slider;//滑動條組件
public Text text;//顯示百分比的文字
public void LoadNextLevel()
{
StartCoroutine(Loadlevel());
}
IEnumerator Loadlevel()
{
loadScreen.SetActive(true);
AsyncOperation operation = SceneManager.LoadSceneAsync("Main");//(SceneManager.GetActiveScene().buildIndex+1);
operation.allowSceneActivation = false;//是否允許加載新場景? 需要加載完自動跳轉(zhuǎn) 就不用添加這句話
while (!operation.isDone)//isDone 是否完成進度條
{
slider.value = operation.progress;
text.text = operation.progress * 100 + "%";//百分比
if (operation.progress>=0.9f)//如果進度條已經(jīng)到達90%
{
slider.value = 1; //那就讓進度條的值編變成1
text.text = "請點擊屏幕繼續(xù)!";
if (Input.anyKey)//如果點擊了任意按鍵
{
operation.allowSceneActivation = true;//就可以跳轉(zhuǎn)場景
}
}
yield return null;
}
}
}
.
三、 最終效果
四、結(jié)束語
不及硅步,無以至千里。
不積小流,無以成江海。
每天進步一點點 謝謝您的觀看。文章來源:http://www.zghlxwxcb.cn/news/detail-407623.html
覺得對自己有幫助,歡迎關(guān)注、收藏、轉(zhuǎn)發(fā)!文章來源地址http://www.zghlxwxcb.cn/news/detail-407623.html
到了這里,關(guān)于手把手教會你Unity場景切換進度條制作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!