当前位置:网站首页>Unity3d-异步加载场景、进度条加载
Unity3d-异步加载场景、进度条加载
2022-07-26 07:10:00 【MaNongSa】
AsyncOperation
提示:以下是本篇文章正文内容
AsyncOperation
描述
异步操作协同程序。
您可以 yield 直到异步操作继续,或手动检查它已完成 (isDone) 还是正在进行 (progress)。
另请参阅:SceneManager.LoadSceneAsync、AssetBundle.LoadAssetAsync、Resources.LoadAsync。
变量
| 变量名 | 作用 |
|---|---|
| allowSceneActivation | 允许在场景准备就绪后立即激活场景。 |
| isDone | 操作是否已完成?(只读) |
| priority | Priority 允许您调整执行异步操作调用的顺序。 |
| progress | 获取操作进度。(只读) |
Events
| 事件 | 作用 |
|---|---|
| completed | 操作完成时调用的事件。即使操作能够同步完成,也将在下一帧调用在创建它的调用所在的帧中注册的事件处理程序。如果处理程序是在操作完成后注册的,并且已调用 complete 事件,则将同步调用该处理程序。 |
代码案例:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class AsyncScene : MonoBehaviour
{
public Text textValue;
public Slider sliderImg;
public GameObject showObj;
AsyncOperation asyncOperation;
/// <summary>
/// 判断能不能异步场景加载好了没
/// </summary>
private bool isLodaScene;
private bool isLodaScene1;
private void Update()
{
if (isLodaScene&& isLodaScene1)
{
if (Input.anyKeyDown)
{
asyncOperation.allowSceneActivation = true;
isLodaScene = false;
}
}
}
public void startToLoadScene()
{
//启动协程
StartCoroutine(AsuncLoad());
}
private IEnumerator AsuncLoad()
{
yield return null;
isLodaScene = true;
//显示加载UI
showObj.SetActive(true);
//获取当前活动的场景的索引+1
asyncOperation = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex+1);
//不允许在场景准备就绪后立即激活场景。
asyncOperation.allowSceneActivation = false;
//操作是否已完成?(只读)
while (!asyncOperation.isDone && isLodaScene)
{
//如3d场景需要加载,这里练习场景没东西所以要模拟舔加延迟
yield return new WaitForSeconds(1f);
//获取操作进度。(只读)赋值Slider上
sliderImg.value = asyncOperation.progress;
//获取操作进度。(只读)赋值Text文本上
textValue.text = asyncOperation.progress * 100 + "%";
if (asyncOperation.progress >= 0.9f )
{
//如3d场景需要加载,这里练习场景没东西所以要模拟舔加延迟
yield return new WaitForSeconds(1f);
sliderImg.value = 1;
textValue.text = "场景以加载完毕,按下任意键跳转场景。";
isLodaScene = false;
}
yield return null;
}
if (sliderImg.value == 1)
{
isLodaScene = true;
isLodaScene1 = true;
}
}
}
边栏推荐
- QT监听socket事件,使用QSocketNotifier类
- Opencv learn read images videos and webcams
- Depth cloning and reflection of typescript class objects
- Opengauss simple version installation error
- Now the metauniverse is no longer an existence that is only applied to the social field
- 常用的cmd指令
- Manifest merger failed with multiple errors, see logs
- Realization of LED water lamp based on C51
- How to delete a statement audit log?
- centos7下的MySQL57版本,遇到一个问题不理解有知道的大shen告知
猜你喜欢

【无标题】转载

Huffman coding principle

File server fastdfs

问题:Can‘t download sh shellcheck. Please install it manually及shell脚本的一些命令使用

MySQL execution plan

Queue assistant | product update log in June 2022

DaemonSet

Airiot IOT platform enables the container industry to build a welding station information monitoring system

What are the basics of getting started with spot silver

Screen: frame paste, 0 fit, full fit
随机推荐
Intention lock
Image annotation software reference
[Star Project] small hat aircraft War (II)
How to delete a statement audit log?
Solve the problem that Chrome browser is tampered with by drug bullies
Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !
Huffman coding principle
MySQL self incrementing lock
Exclusive lock
10 papers of ant security laboratory were included by ccf-a top meeting to explore the realization of AI credibility from the perspective of algorithm
AcWing-每日一题
[749. Isolate virus]
优炫数据库JDBC打开日志方式有哪些
one hundred and twenty-three million one hundred and twenty-three thousand one hundred and twenty-three
[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
Drools (3): drools basic syntax (1)
你了解MySQL都包含哪些“零件“吗?
Do you want to restart the database to replace the license?
Business secret series -- Talking about the evaluation of commercial passwords from the perspective of Party A and Party B (I)
Introduce you to JVM from architecture