当前位置:   article > 正文

Unity学习日记16(场景跳转、异步加载)_unity设置场景编号

unity设置场景编号

目录

游戏场景

 添加要加载的场景/场景序号

场景跳转

输出当前场景名字

部分基础场景操作

异步加载场景/获取加载进度/演示跳转


游戏场景


 添加要加载的场景/场景序号

将需要加载的场景拖拽进去


场景跳转

直接打开该场景

  1. using UnityEngine.SceneManagement;
  2. ……………………
  3. void Start()
  4. {
  5. SceneManager.LoadScene("SampleScene");
  6. }

打开一个场景和附加打开场景

附加打开会将两个场景中的物体叠加在一起

  1. //单个打开
  2. SceneManager.LoadScene("SampleScene", LoadSceneMode.Single);
  3. //附加打开
  4. SceneManager.LoadScene("SampleScene", LoadSceneMode.Additive);

输出当前场景名字

  1. Scene scene = SceneManager.GetActiveScene();
  2. Debug.Log(scene.name);

部分基础场景操作

  1. //获取当前场景名称
  2. Scene scene = SceneManager.GetActiveScene();
  3. Debug.Log(scene.name);
  4. //场景是否已经加载
  5. Debug.Log(scene.isLoaded);
  6. //路径
  7. Debug.Log(scene.path);
  8. //索引
  9. Debug.Log(scene.buildIndex);
  10. //返回场景的对象数量
  11. GameObject[] gos = scene.GetRootGameObjects();
  12. Debug.Log(gos.Length);
  13. //创建新场景
  14. Scene newS= SceneManager.CreateScene("123");
  15. //卸载新场景
  16. SceneManager.UnloadSceneAsync(newS);
  17. //场景管理类:检测多少已加载的活动场景
  18. Debug.Log(SceneManager.sceneCount);

异步加载场景/获取加载进度/演示跳转

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. public class Async : MonoBehaviour
  6. {
  7. AsyncOperation operation;//场景异步加载
  8. float Timer=0;
  9. void Start()
  10. {
  11. StartCoroutine(LoadScene());//开启一个协程
  12. }
  13. IEnumerator LoadScene()
  14. {
  15. operation = SceneManager.LoadSceneAsync("SampleScene");//指定加载的场景,可以是数字,加载场景的序号
  16. operation.allowSceneActivation = false;//不允许场景自动跳转
  17. yield return operation;
  18. }
  19. void Update()
  20. {
  21. Debug.Log("加载进度:"+operation.progress);//加载进度(0~0.9)
  22. if (operation.progress >= 0.89)
  23. {
  24. Debug.Log("场景已加载完毕");
  25. }
  26. Timer += Time.deltaTime; //计数器累加
  27. if (Timer > 5) //5秒后跳转
  28. operation.allowSceneActivation = true;
  29. }
  30. }

其他

按X快捷键让当前编辑物体是处于世界坐标系还是相对坐标系下。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号