场景中转的时候需要一个加载的Loading场景进行过渡,下面介绍一下一个成熟的Loading条的制作,我用的是NGUI关于UGUI后面也会研究一下补充。
1.在父结点下新建一个Anchor在center位置,下面新建一个Panel。
2.在panel下面新建一个LoadingBar,在它身上添加UIProgressBar脚本,还有放图集的脚本就行。
3.在LoadingBar下加添加一个文本显示加载进度,再加上一个填充的图集。
UI制作还是非常简单的,蛋疼的事情就是加载的那个缓冲过程,还有数字的显示逐步增加还是比较难把握的。
1.新建脚本RnMUI_LoadScene,用来做场景的跳转工作,场景名字手动填充。
using UnityEngine;
using System.Collections;
public class RnMUI_LoadScene : MonoBehaviour {
public string sceneName;
public UIProgressBar bar;
public float requiredValue = 1f;
public bool ignoreFirst = true;
public void LoadScene()
{
Application.LoadLevel(sceneName);
}
//给脚本UIProgressbar脚本的OnValueChange赋值
public void OnProgress()
{
if (this.ignoreFirst)
{
this.ignoreFirst = false;
return;
}
if (this.bar != null && this.bar.value >= this.requiredValue)
LoadScene();
}
}
2.新建一个脚本RnMUI_ProgressBar_Test,用来做加载条的加载过程。
using UnityEngine;
using System.Collections;
public class RnMUI_ProgressBar_Test : MonoBehaviour {
public UIProgressBar bar;
public UILabel label;
public UIEasing.EasingType easing = UIEasing.EasingType.easeLinear;
public float duration = 2f;
public float startDelay = 0f;
public float holdTime = 1f;
public bool startFromZero = false;
private bool started = false;
private bool ascending = false;
void Start()
{
if (this.bar == null)
this.bar = this.GetComponent<UIProgressBar>();
if (this.startFromZero)
{
this.ascending = true;
this.bar.value = 0f;
this.UpdateLabel();
}
}
void OnEnable()
{
if (this.bar != null)
this.StartCoroutine("Progress");
}
private IEnumerator Progress()
{
if (!this.started && this.startDelay > 0f)
{
this.started = true;
yield return new WaitForSeconds(this.startDelay);
}
float startTime = Time.time;
while (Time.time <= (startTime + this.duration))
{
float RemainingTime = ((startTime + this.duration) - Time.time);
float ElapsedTime = (this.duration - RemainingTime);
float eased = UIEasing.Ease(this.easing, ElapsedTime, 0f, 1f, this.duration);
// Invert in case of decending
if (!this.ascending)
eased = 1f - eased;
this.bar.value = eased;
this.UpdateLabel();
yield return 0;
}
// Round up the value
this.bar.value = (this.ascending) ? 1f : 0f;
this.UpdateLabel();
if (this.holdTime > 0f)
yield return new WaitForSeconds(this.holdTime);
this.ascending = !this.ascending;
this.StartCoroutine("Progress");
}
public void UpdateLabel()
{
if (this.label != null && this.bar != null)
this.label.text = (this.bar.value * 100).ToString("0") + "%";
}
}
这两个脚本可以直接挂在LoadingBar上,然后就可以测试运行了。