[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
前言:
玩游戏最讨厌的是什么呢?当然是游戏加载界面啦
* 但是你知道吗,加载界面在游戏制作中是不可或缺的,它可以使我们的场景完全加载完毕后再进入游戏,确保用户不会出现加载卡顿的情况。
如何实现一个简易的加载界面呢?来跟我学习一下吧
效果图
UI创建:
首先创建一个开始界面,为Button按钮添加事件来完成场景的转换:
然后创建一个加载界面的UI,首先需要的呢就是一个游戏进度条,可以直接使用Slider来完成创建,其UI层级关系为:
其元素作用为:
* Background:即整个进度条的背景为棕色
* Fill:是加载过的进度条的填充,为蓝色区域
* Hander:是中间的白色方块,为加载临界点
上面三个元素都可以通过修改其上面的Image组件完成对形状的修改,而Hander的位置则是通过Slider
上的接口Value来完成的,同样我们在脚本中可以调用其API:
其次呢,需要为加载界面添加一个Text 文本框,来输出加载百分比
异步加载脚本编写:
实现异步场景加载需要通过AsyncOperation来完成,查阅官方文档,可以发现AsyncOperation
是一个类,其基本属性有如下几种(加黑为本次使用):
* allowSceneActivation:加载完成后,是否允许场景跳转
* isDone:场景是否加载完成
* priority:可以调整异步加载的优先级
* progress:场景加载进度,数值为从0到1
我们知道场景的直接转换是通过SceneManager.LoadScene()来直接跳转完成的,而对于异步加载而言场景的跳转是通过
SceneManager.LoadSceneAsync()来完成的。
两者区别:
查看Unity官方手册
而为了在场景没有加载完之前,就不进行场景跳转,需要使用协程来控制其转换:
public GameObject image; //加载界面 public Slider slider; //进度条 public Text text;
//加载进度文本 public void LoadNextLeaver() { image.SetActive(true); StartCoroutine(
LoadLeaver()); } IEnumerator LoadLeaver() { AsyncOperation operation =
SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex + 1);
//获取当前场景并加一 //operation.allowSceneActivation = false; while(!operation.isDone)
//当场景没有加载完毕 { slider.value = operation.progress; //进度条与场景加载进度对应 text.text = (
operation.progress * 100).ToString()+"%"; yield return null; } }
完成,求三连!