我是靠谱客的博主 俊逸流沙,这篇文章主要介绍场景的异步加载(按钮按下,开始加载),现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class Loading : MonoBehaviour { //滑动条 public Slider LoadingSlider; //显示进度文字 public Text LoadingText; private float loadingSpeed = 1; private float targetValue; //异步操作协同程序 private AsyncOperation operation; private bool isLoading; void Start () { LoadingSlider.value = 0.0f; } public void LoadingMain() { StartCoroutine(AsyncLoading()); isLoading = true; } IEnumerator AsyncLoading() { operation = SceneManager.LoadSceneAsync("main"); //阻止当前加载完成自动切换 operation.allowSceneActivation = false; yield return operation; } void Update() { if (isLoading) { targetValue = operation.progress; //operation.progress异步操作协同程序完成的程度,最大是1 if (operation.progress >= 0.9f) { targetValue = 1.0f; } if (targetValue != LoadingSlider.value) { //插值运算 LoadingSlider.value = Mathf.Lerp(LoadingSlider.value, targetValue, Time.deltaTime * loadingSpeed); //绝对值 if (Mathf.Abs(LoadingSlider.value - targetValue) < 0.01f) { LoadingSlider.value = targetValue; } } LoadingText.text = ((int)(LoadingSlider.value * 100)).ToString() + "%"; if ((int)(LoadingSlider.value * 100) == 100) { //允许异步加载完毕后自动切换场景 operation.allowSceneActivation = true; } } } }

 

最后

以上就是俊逸流沙最近收集整理的关于场景的异步加载(按钮按下,开始加载)的全部内容,更多相关场景内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部