实现思路:
加载器加载场景 -> 加载进度通知UI -> 更新UI进度
1.场景加载器:
SceneLoader.cs
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
/// <summary>
/// 场景加载器
/// <param>ZhangYu 2017-06-15</param>
/// </summary>
public class SceneLoader : MonoBehaviour {
// 场景名称
public string sceneName;
// 进度事件接收者
public Transform progressReceiver;
// 加载进度
private float _progress = 0;
// 异步加载信息
private AsyncOperation async;
/// <summary> 加载当前配置的场景 </summary>
public void load(){
loadScene(sceneName);
}
/// <summary> 加载场景 </summary>
/// <param name="sceneName">场景名称</param>
public void loadScene(string sceneName) {
if (isLoading) return;
this.sceneName = sceneName;
_progress = 0;
enabled = true;
StartCoroutine(loading());
if (progressReceiver != null) {
progressReceiver.gameObject.SetActive(true);
progressReceiver.SendMessage("onProgress", progress, SendMessageOptions.DontRequireReceiver);
}
}
/// <summary> 开启协程加载场景 </summary>
private IEnumerator loading() {
async = SceneManager.LoadSceneAsync(sceneName);
yield return async;
if (progressReceiver != null) progressReceiver.gameObject.SetActive(false);
async = null;
}
#if UNITY_EDITOR
// 屏蔽Animation关键帧事件中的loading()可选项
private IEnumerator loading(string sceneName) {
yield return null;
}
#endif
/// <summary> 正在加载 </summary>
public bool isLoading {
get { return async != null; }
}
/// <summary> 加载进度 </summary>
public float progress {
get { return _progress; }
}
/// <summary> 更新加载进度 </summary>
private void Update() {
if (async == null) {
enabled = false;
} else {
// 加载进度值范围是0~0.9 这里做下处理
float progress = async.progress;
progress = progress < 0.9f ? progress * (10 / 9f) : 1;
_progress = progress;
if (progressReceiver != null) {
progressReceiver.SendMessage("onProgress", progress, SendMessageOptions.DontRequireReceiver);
}
}
}
}
2.进度条:
ProgressBar.cs
using System;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 进度条
/// <param>ZhangYu 2017-06-15</param>
/// </summary>
public class ProgressBar : MonoBehaviour, IOnProgress {
// 进度条
public Slider slider;
// 进度文本
public Text text;
#if UNITY_EDITOR
private void Reset() {
slider = GetComponentInChildren<Slider>();
text = GetComponentInChildren<Text>();
}
#endif
/// <summary> 进度变更事件 </summary>
public void onProgress(float progress) {
if (slider != null) slider.value = progress;
if (text != null) text.text = (int)(progress * 100) + "%";
}
}
3.进度更新接口:
IOnProgress.cs
/// <summary>
/// 进度更新接口
/// <param>ZhangYu 2017-06-15</param>
/// </summary>
public interface IOnProgress {
/// <summary> 进度更新事件 </summary>
/// <param name="progress">进度百分比</param>
void onProgress(float progress);
}
附送场景加载器 编辑器:
SceneLoaderEditor.cs
using UnityEditor;
using UnityEngine;
/// <summary>
/// 场景加载器 编辑器
/// <para>ZhangYu 2018-06-15</para>
/// </summary>
[CanEditMultipleObjects]
[CustomEditor(typeof(SceneLoader))]
public class SceneLoaderEditor : Editor {
public override void OnInspectorGUI() {
// 重绘GUI
SceneLoader loader = (SceneLoader)target;
EditorGUI.BeginChangeCheck();
drawProperty("sceneName", "场景名称");
drawProperty("progressReceiver", "进度事件接收者");
if (loader.isLoading) EditorGUILayout.FloatField("加载进度", loader.progress);
if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties();
}
private void drawProperty(string property, string label) {
EditorGUILayout.PropertyField(serializedObject.FindProperty(property), new GUIContent(label), true);
}
}
附送进度条 编辑器:
ProgressBarEditor.cs
using UnityEditor;
using UnityEngine;
/// <summary>
/// 进度条 编辑器
/// <para>ZhangYu 2018-06-15</para>
/// </summary>
[CanEditMultipleObjects]
[CustomEditor(typeof(ProgressBar))]
public class ProgressBarEditor : Editor {
public override void OnInspectorGUI() {
// 重绘GUI
EditorGUI.BeginChangeCheck();
drawProperty("slider", "进度条");
drawProperty("text", "进度文本");
if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties();
}
private void drawProperty(string property, string label) {
EditorGUILayout.PropertyField(serializedObject.FindProperty(property), new GUIContent(label), true);
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。