赞
踩
就是其他主页点击按钮后先跳转到这个加载页面场景, 同时异步加载要跳转到的场景
参考:Unity SceneManager场景管理Chinar详解API
新建一个加载页面场景
创建一个滑动条用来表示进度条
再创建一个文本用来显示进度百分比
位置随便摆一下
创建一个空对象
在空对象上新建挂载脚本
修改代码:
- /*加载场景页面*/
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;//场景管理
-
- public class Load_test : MonoBehaviour
- {
- // Start is called before the first frame update
- void Start()
- {
- LoadNextLeaver();//开启协程(多线程?)
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
-
- public GameObject image; //加载界面
- public Slider slider; //进度条
- public Text text; //加载进度文本
-
- public void LoadNextLeaver()
- {
- image.SetActive(true);
- StartCoroutine(LoadLeaver());
- }
- IEnumerator LoadLeaver()
- {
- AsyncOperation operation = SceneManager.LoadSceneAsync(2); //准备加载序号为2的场景
- operation.allowSceneActivation = true;//加载完成后,是否允许场景跳转
- while (!operation.isDone) //当场景没有加载完毕
- {
- slider.value = operation.progress; //进度条与场景加载进度对应
- text.text = (operation.progress * 100).ToString() + "%";
- yield return null;
- }
- }
- }
-
-
-
运行测试,可以实现跳转和进度条
但目标场景太小了,瞬间闪一下就加载完了,就很出戏
一个伪加载实现:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- using UnityEngine.UI;
-
- public class Load_test_ : MonoBehaviour
- {
- // Start is called before the first frame update
- void Start()
- {
-
- }
-
- // Update is called once per frame
- //void Update()
- //{
-
- //}
-
- /// <summary>
- /// 进度条下方显示的文本
- /// </summary>
- [SerializeField]
- Text Aegis_text;
- /// <summary>
- /// 进度条
- /// </summary>
- [SerializeField]
- Slider slider;
- /// <summary>
- /// 文字后方点数显示
- /// </summary>
- float pointCount;
- /// <summary>
- /// 当前进度
- /// </summary>
- float progress = 0;
- /// <summary>
- /// 进度条读取完成时间
- /// </summary>
- float total_time = 3f;
- /// <summary>
- /// 计时器
- /// </summary>
- float time = 0;
- void OnEnable()
- {
- //开启协程
- StartCoroutine("AegisAnimation");
- }
- void Update()
- {
- //记录时间增量
- time += Time.deltaTime;
- //当前进度随着时间改变的百分比
- progress = time / total_time;
- if (progress >= 1)
- {
- UnityEngine.SceneManagement.SceneManager.LoadScene(2);//假装的加载完成后,还是要跳转到目标场景
- return;
- }
- //把进度赋给进度条的值
- slider.value = progress;
- }
- void OnDisable()
- {
- //关闭协程
- StopCoroutine("AegisAnimation");
- }
- /// <summary>
- /// 文本显示协程
- /// </summary>
- /// <returns></returns>
- IEnumerator AegisAnimation()
- {
-
- while (true)
- {
- yield return new WaitForSeconds(0.1f);
- float f = slider.value;
- //设置进度条的value值在某个区间的时候要显示的字符串
- string reminder = "";
- if (f < 0.25f)
- {
- reminder = "检测余额中...";
- }
- else if (f < 0.5f)
- {
- reminder = "注入木马中...";
- }
- else if (f < 0.75f)
- {
- reminder = "破解密码中...";
- }
- else
- {
- reminder = "上传数据中...";
- }
- //显示字符串后面的“.”
- pointCount++;
- if (pointCount == 7)
- {
- pointCount = 0;
- }
- for (int i = 0; i < pointCount; i++)
- {
- reminder += ".";
- }
- //把显示内容赋给场景中的text
- Aegis_text.text = reminder;
- }
- }
- }
测试效果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。