当前位置:网站首页>Unity3d asynchronous loading of scenes and progress bar loading
Unity3d asynchronous loading of scenes and progress bar loading
2022-07-26 07:14:00 【MaNongSa】
AsyncOperation
Tips : The following is the main body of this article
AsyncOperation
describe
Asynchronous operation synergist .
You can yield Until the asynchronous operation continues , Or manually check that it is complete (isDone) Still in progress (progress).
See also :SceneManager.LoadSceneAsync、AssetBundle.LoadAssetAsync、Resources.LoadAsync.
Variable
| Variable name | effect |
|---|---|
| allowSceneActivation | Allows the scene to be activated immediately after it is ready . |
| isDone | Is the operation complete ?( read-only ) |
| priority | Priority Allows you to adjust the order in which asynchronous operation calls are performed . |
| progress | Get the operation progress .( read-only ) |
Events
| event | effect |
|---|---|
| completed | The event called when the operation is completed . Even if the operation can be completed synchronously , The event handler registered in the frame of the call that created it will also be called at the next frame . If the handler is registered after the operation is completed , And called complete event , The handler will be called synchronously . |
Code case :
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>
/// Judge whether the asynchronous scene can be loaded
/// </summary>
private bool isLodaScene;
private bool isLodaScene1;
private void Update()
{
if (isLodaScene&& isLodaScene1)
{
if (Input.anyKeyDown)
{
asyncOperation.allowSceneActivation = true;
isLodaScene = false;
}
}
}
public void startToLoadScene()
{
// Start the coroutines
StartCoroutine(AsuncLoad());
}
private IEnumerator AsuncLoad()
{
yield return null;
isLodaScene = true;
// According to load UI
showObj.SetActive(true);
// Get the index of the currently active scene +1
asyncOperation = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex+1);
// It is not allowed to activate the scene immediately after the scene is ready .
asyncOperation.allowSceneActivation = false;
// Is the operation complete ?( read-only )
while (!asyncOperation.isDone && isLodaScene)
{
// Such as 3d The scene needs to be loaded , There is nothing in the practice scene here, so we need to simulate licking and delay
yield return new WaitForSeconds(1f);
// Get the operation progress .( read-only ) assignment Slider On
sliderImg.value = asyncOperation.progress;
// Get the operation progress .( read-only ) assignment Text On the text
textValue.text = asyncOperation.progress * 100 + "%";
if (asyncOperation.progress >= 0.9f )
{
// Such as 3d The scene needs to be loaded , There is nothing in the practice scene here, so we need to simulate licking and delay
yield return new WaitForSeconds(1f);
sliderImg.value = 1;
textValue.text = " The scene is loaded , Press any key to jump to the scene .";
isLodaScene = false;
}
yield return null;
}
if (sliderImg.value == 1)
{
isLodaScene = true;
isLodaScene1 = true;
}
}
}
边栏推荐
- Leetcode 1184:公交站间的距离
- 强网杯2021 pwn 赛题解析——baby_diary
- [arm learning (8) AXF tool analysis]
- Opencv learning warp Perspective
- 达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程【三】
- 中国联通改造 Apache DolphinScheduler 资源中心,实现计费环境跨集群调用与数据脚本一站式访问
- Solve the problem that Chrome browser is tampered with by drug bullies
- Drools(3):Drools基础语法(1)
- 常用的cmd指令
- Why can't extern compile variables decorated with const?
猜你喜欢

To do list application vikunja

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

404 page best practices to improve user experience

Apache DolphinScheduler&TiDB联合Meetup | 聚焦开源生态发展下的应用开发能力

Flame diagram analysis Flink backpressure

Can you learn fast and well with dual stream network? Harbin Institute of Technology & Microsoft proposed a distillation dual encoder model for visual language understanding, which can achieve fast an
![[yiku] wonderful! This library organization NPM script is simply explosive!](/img/84/e118c11f27aaf26e52059cb7ebc27a.png)
[yiku] wonderful! This library organization NPM script is simply explosive!

文件服务器FastDFS

Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !

How can man Bang write a new footnote to the value of the industry's first social responsibility report?
随机推荐
Embedded development: tools -- intelligent watchdog design
又是一年开源之夏,1.2万项目奖金等你来拿!
A guide for you to fully use TS
How to delete a statement audit log?
Hands on practice - teach you how to make an intelligent fish tank with STM32
PG operation and maintenance -- logical backup and physical backup practice
Rectification ideas for the previous article
Agile and tidy way
Advanced Mathematics (Seventh Edition) Tongji University General exercises two person solution
Intention lock
【QT】详解 *.pro、*.pri、*.prf、*.prl文件
C51与MDK共存 Keil5安装教程
敏捷整洁之道
Basic operations and common functions of MySQL table creation
Solve the problem that Chrome browser is tampered with by drug bullies
Kernel pwn 入门 (5)
Leetcode 1184:公交站间的距离
问题:Can‘t download sh shellcheck. Please install it manually及shell脚本的一些命令使用
LTS(Light-Task-Scheduler)
WPS or office compression of ppt