须要三个场景,场景A,场景B,场景C;canvas
场景A:一个按钮,点击加载场景B;this
场景B:从A切换到C过分场景,加载进度条;spa
场景C:目标场景;3d
建立OnProgress.cs脚本:code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class OnProgress : MonoBehaviour { public void OnBtnClick() { Debug.Log("clicked"); Globe.nextSceneName = "MainScene";//目标场景名称 SceneManager.LoadScene("Loading");//加载进度条场景 } }
建立一个panel,在panel下建立一个button,将OnProgress脚本挂载到canvas,点击button,设置button属性,绑定脚本方法,点击加号,选择canvas中刚才绑定脚本中的方法OnBtnClick。至此,A场景完成。协程
建立B场景Loading:对象
Loading场景由两部分组成,加载进度百分比和进度条:blog
文本就不说了,说明下进度条的实现,进度条实际是一个Image,设置Image Type为filled,fill Method为horizonal,这里必定要添加source Image,不然下面的Image Type不会出来。图片
另外说下添加图片,普通的图片添加到assets中不能直接添加到Source Image中,须要对图片进行设置,以下图:string
OK,再看进度条加载过程的实现:
建立AsyncLoadScene脚本:
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class Globe { public static string nextSceneName; } public class AsyncLoadScene : MonoBehaviour { public Text loadingText; public Image progressBar; private int curProgressValue = 0; private AsyncOperation operation; // Use this for initialization void Start() { if (SceneManager.GetActiveScene().name == "Loading") { //启动协程 StartCoroutine(AsyncLoading()); } } IEnumerator AsyncLoading() { operation = SceneManager.LoadSceneAsync(Globe.nextSceneName); //阻止当加载完成自动切换 operation.allowSceneActivation = false; yield return operation; } // Update is called once per frame void Update() { int progressValue = 100; if (curProgressValue < progressValue) { curProgressValue++; } loadingText.text = curProgressValue + "%";//实时更新进度百分比的文本显示 progressBar.fillAmount = curProgressValue / 100f;//实时更新滑动进度图片的fillAmount值 if (curProgressValue == 100) { operation.allowSceneActivation = true;//启用自动加载场景 loadingText.text = "OK";//文本显示完成OK } } }
将脚本挂在到Loading场景的camera上面。设置对象:
这样进度条加载场景就完成了,C场景就不介绍了,就是你要跳转的目标场景。