当前位置:网站首页>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;
}
}
}
边栏推荐
- Analysis of strong tennis cup 2021 PWN competition -- baby_ diary
- 优炫数据库JDBC打开日志方式有哪些
- 【数据库】CTE(Common Table Expression(公共表表达式))
- [arm learning (8) AXF tool analysis]
- QT监听socket事件,使用QSocketNotifier类
- npm 命令
- Intention lock
- Business secret series -- Talking about the evaluation of commercial passwords from the perspective of Party A and Party B (I)
- Opengauss simple version installation error
- Benefits of the builder model
猜你喜欢

文件服务器FastDFS

mySql建表的基本操作 与常见的函数

哈夫曼编码原理

Rectification ideas for the previous article

Solve the problem that Chrome browser is tampered with by drug bullies

常用的cmd指令
![[arm learning (8) AXF tool analysis]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[arm learning (8) AXF tool analysis]

【硬十宝典】——7.1【动态RAM】DDR硬件设计要点

Drools (4): drools basic syntax (2)

Airiot IOT platform enables the container industry to build a welding station information monitoring system
随机推荐
Yolov6 target detection practice: training your own data set (video tutorial)
Drools (2): drools quick start
Check the top 10 best graphics software of the year, meet 99% of your chart needs, and collect it quickly
docker修改挂载到宿主机上的mysql配置文件不生效?
MySQL intent lock
Opencv learn resize and crop
Rectification ideas for the previous article
Is it safe to invest in treasury bonds in 2022? How do individuals buy treasury bonds?
MySQL table write lock
Idea -- use @slf4j to print logs
buuReserve(4)
Intention lock
[hard ten treasures] - 7.2 [dynamic RAM] analysis of the difference between DDR4 and DDR3
哈夫曼编码原理
IDEA——使用@Slf4j打印日志
Opencv learning color detection
Image annotation software reference
Solve the problem that Chrome browser is tampered with by drug bullies
文件服务器FastDFS
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