unity中实现场景之间加载进度条
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadingScene : MonoBehaviour
{
public Image progressImg;
private AsyncOperation async;
public Text text;
private int curProgressVaule = 0;//计数器
// Use this for initialization
void Start ()
{
StartCoroutine(LoadScene());
}
IEnumerator LoadScene()
{
async = SceneManager.LoadSceneAsync("game");//异步跳转到game场景
async.allowSceneActivation = false;//当game场景加载到90%时,不让它直接跳转到game场景。
yield return async;
}
// Update is called once per frame
void Update () {
if (async == null)
{
return;
}
int progressVaule = 0;
if (async.progress < 0.9f)
{
progressVaule = (int) async.progress*100;
}
else
{
progressVaule = 100;
}
if (curProgressVaule < progressVaule)
{
curProgressVaule++;
}
text.text = curProgressVaule + "%";
progressImg.fillAmount = curProgressVaule/100f;
if (curProgressVaule == 100)
{
async.allowSceneActivation = true;
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步