UnityUI加载进度条
初学者的写法:
System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class NewBehaviourScript : MonoBehaviour {
public float UI_Wight;
public float UI_Height;
float process;
float _process;
void Start ()
{
}
void Update ()
{
}
void OnGUI()
{
GUI.color = Color.red;
if(GUI.Button(new Rect(Screen.width / 2 - UI_Wight / 2, Screen.height / 2 - UI_Height / 2, UI_Wight, UI_Height),"开始游戏"))
{
StartCoroutine("LoadSceneFromBtn");
}
GUILayout.Label("场景加载进度" + _process *100 +"%");
}
IEnumerator LoadSceneFromBtn()
{
AsyncOperation ao= SceneManager.LoadSceneAsync(1);
ao.allowSceneActivation = false;
while (_process<1.0f)
{
process =ao.progress;
if (_process >= 0.9f)
{
_process= Mathf.MoveTowards(_process, 1.0f, 0.01f);
}
else
{
_process= Mathf.MoveTowards(_process, process, 0.01f);
}
yield return 0;
}
ao.allowSceneActivation = true;
}
}
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!