Unity——Loading界面-异步加载场景
2021-11-01 17:25
1556 查看
Loading界面-异步加载场景
Demo展示
加载新场景时,需要加载大量模型外部文件等,比较耗时,又不能让画面卡住不动;
很多时候会做一个加载界面显示读条;
代码
非常简直,就不赘述了;
一个slider条,progress为异步百分比;
限制读条3s结束,小于3s等待;
注意异步读条progress到不了1,所以0.9判断加载完成;
public class Loading : MonoBehaviour { public string scenceName; AsyncOperation async; public Slider m_pProgress; private int progress = 0; private float time = 3f; private float startTime = 0; private void Start() { StartCoroutine(LoadScenes()); startTime = Time.time; } IEnumerator LoadScenes() { int nDisPlayProgress = 0; async = SceneManager.LoadSceneAsync(scenceName); async.allowSceneActivation = false; while (async.progress < 0.9f) { progress = (int)async.progress * 100; while (nDisPlayProgress < progress) { ++nDisPlayProgress; m_pProgress.value = (float)nDisPlayProgress / 100; yield return new WaitForEndOfFrame(); } yield return null; } progress = 100; while (nDisPlayProgress < progress) { ++nDisPlayProgress; m_pProgress.value = (float)nDisPlayProgress / 100; yield return new WaitForEndOfFrame(); } float temp = Time.time - startTime; if (temp < time) yield return new WaitForSeconds(time - temp); async.allowSceneActivation = true; } }
测试代码:
加载场景时,记在loading界面,设置需要加载的场景名称;
public class MainScence : MonoBehaviour { public Button btnLoad; void Start() { btnLoad.onClick.AddListener(OnLoadFight); } private void OnLoadFight() { GameObject go = Instantiate(Resources.Load<GameObject>("Loading"), transform); go.GetComponent<Loading>().scenceName = "FightScence"; } }
相关文章推荐
- 异步加载场景loading界面
- Unity3D——异步加载游戏场景loading界面的开发!
- Unity3D——异步加载游戏场景loading界面的开发
- 玲珑杯Unity开发心得——进度条界面(异步加载游戏场景)
- Unity:异步加载场景资源进度界面
- [置顶] unity中关于异步loading场景的加载
- UNITY 异步加载场景的一个例子
- Unity场景跳转异步加载
- 【Unity】加载Loading场景进度条slider平滑问题
- Unity 异步加载场景
- 三步搞定Unity异步加载场景
- unity5.3新API使用进度条,异步加载场景的代码
- unity3d学习笔记(十五)--利用Loading界面异步过渡游戏场景
- unity3d学习笔记(十五)--利用Loading界面异步过渡游戏场景
- cocos2dx异步加载资源制作Loading界面
- Unity异步加载场景与加载进度条
- Unity异步加载场景loading条
- unity场景切换异步加载
- 异步加载资源的loading界面
- 场景异步加载显示加载界面