这里写自定义目录标题
把这段代码挂载到Scence(0)上边
然后在主界面拥有的GameObject:
默认背景 Image BG;
开始按钮 Button startButton;
然后是 Image LoadingPanelBG;
LoadingPanelBG子集有一个进度条的Slider
然后在空物体上挂载这个组件 把方法注册到Button的点击事件中。
这样 就可以自动跳转到第二个界面了!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadScene : MonoBehaviour
{
public Slider slider; //进度条的滑动条
int currentProgress; //当前进度
int targetProgress; //目标进度
// public Button startGameButton;//startGameButton推荐在外边赋值
public Image loadpamel; //在这个image里边赋值上loadingPanel的图片 默认让他隐藏
private void Awake()
{
Screen.SetResolution(668, 904, false);//设定分辨率
// Screen.SetResolution(1024, 1366, false);
}
public void load() //外部StartGameButton绑定的监听事件
{
loadpamel.gameObject.SetActive(true); //点击之后显示loadingPanel
currentProgress = 0; //暂时让当前进度为0 目标进度也是0
targetProgress = 0;
StartCoroutine(LoadingScene()); //开启加载进度的携程
}
/// <summary>
/// 异步加载场景
/// </summary>
/// <returns>协成</returns>
IEnumerator LoadingScene()//加载进度的携程启动
{
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(1); //异步加载1号场景 返回 AsyncOperation类的 asyncOperation
asyncOperation.allowSceneActivation = false; //不允许场景立即激活//异步进度在 allowSceneActivation= false时,会卡在0.89999的一个值,这里乘以100转整形
while (asyncOperation.progress < 0.9f) //当异步加载小于0.9f的时候
{
targetProgress = (int)(asyncOperation.progress * 100); //异步进度在 allowSceneActivation= false时,会卡在0.89999的一个值,这里乘以100转整形
yield return new UnityEngine.WaitForEndOfFrame();
while (currentProgress < targetProgress) //当前进度 < 目标进度时
{
currentProgress += 20; //当前进度不断累加 (Chinar温馨提示,如果场景很小,可以调整这里的值 例如:+=10 +=20,来调节加载速度)
slider.value = (float)currentProgress / 100; //给UI进度条赋值
yield return new WaitForEndOfFrame(); //等一帧
}
}
targetProgress = 100; //循环后,当前进度已经为90了,所以需要设置目标进度到100;继续循环
yield return new WaitForEndOfFrame();
while (currentProgress < targetProgress) //当前进度 < 目标进度时
{
currentProgress += 20; //当前进度不断累加 (Chinar温馨提示,如果场景很小,可以调整这里的值 例如:+=10 +=20,来调节加载速度)
slider.value = (float)currentProgress / 100; //给UI进度条赋值
yield return new WaitForEndOfFrame(); //等一帧
}
asyncOperation.allowSceneActivation = true; //加载完毕,这里激活场景 —— 跳转场景成功
}
/// <summary>
/// 由于需要两次调用,在这里进行简单封装
/// </summary>
/// <returns>等一帧</returns>
IEnumerator<WaitForEndOfFrame> LoadProgress()
{
while (currentProgress < targetProgress) //当前进度 < 目标进度时
{
currentProgress += 20; //当前进度不断累加 (Chinar温馨提示,如果场景很小,可以调整这里的值 例如:+=10 +=20,来调节加载速度)
slider.value = (float)currentProgress / 100; //给UI进度条赋值
yield return new WaitForEndOfFrame(); //等一帧
}
}
}