当前位置: 首页 > news >正文

unity学习24:场景scene相关生成,加载,卸载,加载进度,异步加载场景等

目录

1 场景数量 SceneManager.sceneCount

2 直接代码生成新场景 SceneManager.CreateScene

3 场景的加载

3.1 用代码加载场景,仍然build setting里先加入配置

3.2 卸载场景 SceneManager.UnloadSceneAsync();

3.3 同步加载场景 SceneManager.LoadScene

3.3.1  两种加载方式

3.4 异步加载场景

3.5 测试代码

3.5.1 有问题的测试代码

(代码创建的新Scene 需要手动去build Setting添加?)

3.6 场景的叠加效果

4 同步和异步

4.1  同步和异步

4.2 多线程  协程

4.2.1 多线程

4.2.2 协程

4.3 异步加载场景UnityEngine.SceneManagement;

4.3.1 除了默认的,还需要额外导入其他包

4.3.2  测试异步跳转场景,可成功

4.3.3 详细代码和注释

4.3.4 注释内容

5 加载进度

5.1  对应的协程的进度  operation1.progress

5.2  关于进度的数值

5.3 对应代码

5.4 如何做个显示的UI进度条

6 按条件跳转新地图

6.1 延迟跳转

6.2 测试代码


1 场景数量 SceneManager.sceneCount

  • //统计已经加载的场景数量
  • Debug.Log(SceneManager.sceneCount);

2 直接代码生成新场景 SceneManager.CreateScene

  • // 代码里可以创建新场景:直接用代码
  • Scene scene3=SceneManager.CreateScene("Scene3");

3 场景的加载

3.1 用代码加载场景,仍然build setting里先加入配置

  • 也是要注意,新场景的加载模式
  • 用代码创建的Scene也可以现在build setting里先加入配置

Scene 'Scene3' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
To add a scene to the build settings use the menu File->Build Settings...
UnityEngine.SceneManagement.SceneManager:LoadScene (string)
SceneTest:Start () (at Assets/SceneTest.cs:43)

3.2 卸载场景 SceneManager.UnloadSceneAsync();

  • 卸载场景
  • SceneManager.UnloadSceneAsync("Scene3");

3.3 同步加载场景 SceneManager.LoadScene

  • //同步加载场景,卡顿,等待
  • SceneManager.LoadScene("Scene3");
  • SceneManager.loadScene("scene3",LoadSceneMode.Single);
  • SceneManager.loadScene("scene3",LoadSceneMode.Additive);

3.3.1  两种加载方式

只加载1个,替换之前的Scene

  • SceneManager.LoadScene("Scene2")  
  • 默认方式是 LoadSceneMode.Single
  • SceneManager.LoadScene("Scene2",LoadSceneMode.Single)  

新的场景加载,老的也在,相当于同时都加载生效

  • SceneManager.LoadScene("Scene2",LoadSceneMode.Additive) 

3.4 异步加载场景

  • //异步加载场景
  • //SceneManager.loadSceneAsync("scene3");

3.5 测试代码

3.5.1 有问题的测试代码

(代码创建的新Scene 需要手动去build Setting添加?)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class SceneTest : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){///先查看当前Scene//获取当前场景Scene scene1=SceneManager.GetActiveScene();//场景名称Debug.Log(scene1.name);//场景路径Debug.Log(scene1.path);      //场景索引Debug.Log(scene1.buildIndex);   GameObject[] gb1=scene1.GetRootGameObjects();Debug.Log(gb1.Length);//跳转场景//SceneManager.LoadScene(2);//SceneManager.LoadScene("Scene2");//调用异步的Start1Start1();//统计已经加载的场景数量Debug.Log(SceneManager.sceneCount);//创建新场景:直接用代码Scene scene3=SceneManager.CreateScene("Scene3");//卸载场景//SceneManager.UnloadSceneAsync("Scene3");//同步加载场景,卡顿,等待SceneManager.LoadScene("Scene3");//SceneManager.loadScene("scene3",LoadSceneMode.Single);//SceneManager.loadScene("scene3",LoadSceneMode.Additive);//异步加载场景//SceneManager.loadSceneAsync("scene3");}async void Start1(){AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(2);// 等待场景加载完成while (!asyncLoad.isDone){await System.Threading.Tasks.Task.Yield();}// 场景加载完成后获取信息Debug.Log(SceneManager.GetActiveScene().name);//获取当前场景//新定义1个scene2  Scene scene2=Scene scene2=SceneManager.GetActiveScene();//场景是否已经加载, 但是可能还没有激活新的SceneDebug.Log(scene2.isLoaded); ///再次查看当前Scene//场景名称Debug.Log(scene2.name);//场景路径Debug.Log(scene2.path);      //场景索引Debug.Log(scene2.buildIndex);   GameObject[] gb2=scene2.GetRootGameObjects();Debug.Log(gb2.Length);}// Update is called once per framevoid Update(){}
}

3.6 场景的叠加效果

  • 多个场景一起生效,叠加,效果
  • 从 Hierarchy 窗口里可以看到
  • 现在有3个场景,同时生效了,场景内的gb也都显示出来了

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class SceneTest : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){///先查看当前Scene//获取当前场景Scene scene1=SceneManager.GetActiveScene();//场景名称Debug.Log(scene1.name);//场景路径Debug.Log(scene1.path);      //场景索引Debug.Log(scene1.buildIndex);   GameObject[] gb1=scene1.GetRootGameObjects();Debug.Log(gb1.Length);//跳转场景//SceneManager.LoadScene(2);//SceneManager.LoadScene("Scene2");//调用异步的Start1Start1();//统计已经加载的场景数量Debug.Log(SceneManager.sceneCount);//创建新场景:直接用代码Scene scene3=SceneManager.CreateScene("Scene3");//卸载场景//SceneManager.UnloadSceneAsync("Scene3");//同步加载场景,卡顿,等待SceneManager.LoadScene("Scene3");//SceneManager.loadScene("scene3",LoadSceneMode.Single);//SceneManager.loadScene("scene3",LoadSceneMode.Additive);//异步加载场景//SceneManager.loadSceneAsync("scene3");}async void Start1(){AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(2,LoadSceneMode.Additive);// 等待场景加载完成while (!asyncLoad.isDone){await System.Threading.Tasks.Task.Yield();}// 场景加载完成后获取信息Debug.Log(SceneManager.GetActiveScene().name);//获取当前场景//新定义1个scene2  Scene scene2=Scene scene2=SceneManager.GetActiveScene();//场景是否已经加载, 但是可能还没有激活新的SceneDebug.Log(scene2.isLoaded); ///再次查看当前Scene//场景名称Debug.Log(scene2.name);//场景路径Debug.Log(scene2.path);      //场景索引Debug.Log(scene2.buildIndex);   GameObject[] gb2=scene2.GetRootGameObjects();Debug.Log(gb2.Length);}// Update is called once per framevoid Update(){}
}

4 同步和异步

4.1  同步和异步

  • 同步: 纯线性
  • 异步: 也是线性,但是只是逻辑上还是线性。但是有了分支,把消耗时间的操作,放到其他线程里去执行

下面几个图都是网上找的参考的

4.2 多线程  协程

  • 多线程
  • 协程

4.2.1 多线程

多线程是一种同时运行多个执行路径的技术,每个执行路径称为一个线程。

多个线程可以在多核CPU上真正并行运行,或者在单核CPU上通过时间片轮转模拟并发。多线程通过操作系统调度,能够充分利用计算资源,在处理I/O密集型和CPU密集型任务时具有优势。

特点:
每个线程都有独立的栈空间和执行路径。
线程之间可以共享内存数据,因此需要进行同步控制,以避免数据竞争和死锁问题。
线程调度由操作系统控制,可能涉及上下文切换,带来一定的开销。

4.2.2 协程

协程是一种比线程更轻量级的并发实现方式。

与多线程不同,协程不是由操作系统调度,而是由编程语言或运行时环境来管理。

协程可以在需要时暂停自身,并将控制权交还给调用方,稍后再恢复执行。

它们适用于处理需要频繁暂停和恢复的任务,如异步I/O操作。

特点:
协程不会并行运行,单个线程中可以运行多个协程。
协程之间共享执行线程,但不需要上下文切换,切换开销非常小。
协程适用于I/O密集型任务,如网络请求、文件读写等,能够实现高效的异步操作。

4.3 异步加载场景UnityEngine.SceneManagement;

4.3.1 除了默认的,还需要额外导入其他包

using UnityEngine;

using UnityEngine.SceneManagement;

4.3.2  测试异步跳转场景,可成功

 去掉各种注释的代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//导入场景管理类
using UnityEngine.SceneManagement;public class AsyncTest : MonoBehaviour
{void Start(){StartCoroutine(loadScene());}IEnumerator loadScene(){// 异步的协程AsyncOperation operation1=SceneManager.LoadSceneAsync(2);yield return operation1;}// Update is called once per framevoid Update(){}
}

4.3.3 详细代码和注释

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//导入场景管理类
using UnityEngine.SceneManagement;public class AsyncTest : MonoBehaviour
{//异步,需要先声明SceneManager.LoadSceneAsync(2)的返回值//AsyncOperation operation1;// Start is called before the first frame updatevoid Start(){//协程不能直接LoadScene(),必须新建一个协程方法StartCoroutineStartCoroutine(loadScene());}//以协程的方法来异步加载场景//必须单独写一个 loadScene()方法,而不能用Application.LoadScene()方法//这个异步的方法,有返回值,返回值是固定的IEnumeratorIEnumerator loadScene(){// 异步的协程// SceneManager.LoadSceneAsync()有返回值,返回值类型AsyncOperationAsyncOperation operation1=SceneManager.LoadSceneAsync(2);yield return operation1;}// Update is called once per framevoid Update(){}
}

4.3.4 注释内容

     //异步,需要先声明SceneManager.LoadSceneAsync(2)的返回值

    // 也是可以在函数内,使用 operation1 时当时声明,  但是函数内声明的,函数外就无法条用operation1,所以为了外面可以调用,还是在外面声明

    //AsyncOperation operation1;

    // Start is called before the first frame update

    void Start()

    {

        //协程不能直接LoadScene(),必须新建一个协程方法StartCoroutine

        StartCoroutine(loadScene());

       

    }

    //以协程的方法来异步加载场景

    //必须单独写一个 loadScene()方法,而不能用Application.LoadScene()方法

    //这个异步的方法,有返回值,返回值是固定的IEnumerator

    IEnumerator loadScene()

    {

        // 异步的协程

        // SceneManager.LoadSceneAsync()有返回值,返回值类型AsyncOperation

       // 也可以在函数外,最开始声明

        AsyncOperation operation1=SceneManager.LoadSceneAsync(2);

        yield return operation1;

    }

5 加载进度

5.1  对应的协程的进度  operation1.progress

  • operation1.progress
  • 适合放在 void Update() 方法里去实现,因为逐帧加载。

5.2  关于进度的数值

  • unity里,输出进度 0-0.9,其中0.9 在unity里就是100%
  • 实际游戏里
  • 有的0.9-100%飞速,有的是按比例0.9折算到100%
  • 场景小异步加载也就很快

5.3 对应代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//导入场景管理类
using UnityEngine.SceneManagement;public class AsyncTest : MonoBehaviour
{//异步,需要先声明SceneManager.LoadSceneAsync(2)的返回值AsyncOperation operation1;// Start is called before the first frame updatevoid Start(){//协程不能直接LoadScene(),必须新建一个协程方法StartCoroutineStartCoroutine(loadScene());}//以协程的方法来异步加载场景//必须单独写一个 loadScene()方法,而不能用Application.LoadScene()方法//这个异步的方法,有返回值,返回值是固定的IEnumeratorIEnumerator loadScene(){// 异步的协程// SceneManager.LoadSceneAsync()有返回值,返回值类型AsyncOperationoperation1=SceneManager.LoadSceneAsync(2);yield return operation1;}// Update is called once per framevoid Update(){Debug.Log(operation1.progress);}
}

5.4 如何做个显示的UI进度条

。。。。是个问题,学到了再说

6 按条件跳转新地图

6.1 延迟跳转

  • 跳转地图也可以不是即可生效的
  • 可以加定时器, 按任意键跳转,下面的例子可以实现10秒后跳转
  • 除了延迟跳转新地图,也可以设置其他条件:比如 响应键盘,UI等等

6.2 测试代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//导入场景管理类
using UnityEngine.SceneManagement;public class AsyncTest : MonoBehaviour
{//异步,需要先声明SceneManager.LoadSceneAsync(2)的返回值AsyncOperation operation1;float timer1=0;// Start is called before the first frame updatevoid Start(){//协程不能直接LoadScene(),必须新建一个协程方法StartCoroutineStartCoroutine(loadScene());}//以协程的方法来异步加载场景//必须单独写一个 loadScene()方法,而不能用Application.LoadScene()方法//这个异步的方法,有返回值,返回值是固定的IEnumeratorIEnumerator loadScene(){// 异步的协程// SceneManager.LoadSceneAsync()有返回值,返回值类型AsyncOperationoperation1=SceneManager.LoadSceneAsync(2);operation1.allowSceneActivation=false;yield return operation1;}// Update is called once per framevoid Update(){Debug.Log(operation1.progress);timer1=timer1+Time.deltaTime;if (timer1>10){operation1.allowSceneActivation=true;}}
}

http://www.lryc.cn/news/529588.html

相关文章:

  • [cg] 使用snapgragon 对UE5.3抓帧
  • 一元函数微积分的几何应用:二维平面光滑曲线的曲率公式
  • ISBN 号码——蓝桥杯
  • Spring Boot - 数据库集成06 - 集成ElasticSearch
  • 51单片机CLD1602显示万年历+闹钟+农历+整点报时
  • C++ 中的类(class)和对象(object)
  • 安卓通过网络获取位置的方法
  • 2025 年,链上固定收益领域迈向新时代
  • npm启动前端项目时报错(vue) error:0308010C:digital envelope routines::unsupported
  • 11.QT控件:输入类控件
  • deepseek核心技术:MLA架构-多头潜在注意力
  • 讯飞星火大模型API使用Python调用
  • C#面试常考随笔7:什么是匿名⽅法?还有Lambda表达式?
  • Elasticsearch:如何搜索含有复合词的语言
  • 【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】1.25 视觉风暴:NumPy驱动数据可视化
  • idea maven本地有jar包,但还要从远程下载
  • C++编程语言:抽象机制:模板(Bjarne Stroustrup)
  • 深入解析 Linux 内核中的页面错误处理机制
  • 【AIGC专栏】AI在自然语言中的应用场景
  • Ubuntu 20.04安装Protocol Buffers 2.5.0
  • 解锁豆瓣高清海报(一) 深度爬虫与requests进阶之路
  • 计算机组成原理——数据运算与运算器(二)
  • SpringBoot+Vue的理解(含axios/ajax)-前后端交互前端篇
  • 【AI】DeepSeek 概念/影响/使用/部署
  • javascript-es6 (二)
  • 供应链系统设计-供应链中台系统设计(十四)- 清结算中心设计篇(三)
  • 【自学笔记】MySQL的重点知识点-持续更新
  • X86路由搭配rtl8367s交换机
  • Linux环境基础开发工具的使用(apt, vim, gcc, g++, gbd, make/Makefile)
  • 多模态论文笔记——ViViT