鹤壁网站seo优化,上海市城市建设工程学校网站,邢台做wap网站,网站哪里备案有区别么目录
1 场景数量 SceneManager.sceneCount
2 直接代码生成新场景 SceneManager.CreateScene
3 场景的加载
3.1 用代码加载场景#xff0c;仍然build setting里先加入配置
3.2 卸载场景 SceneManager.UnloadSceneAsync();
3.3 同步加载场景 SceneManager.LoadScene
3.3.…目录
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 scene3SceneManager.CreateScene(Scene3); 3 场景的加载
3.1 用代码加载场景仍然build setting里先加入配置
也是要注意新场景的加载模式用代码创建的Scene也可以现在build setting里先加入配置 Scene Scene3 couldnt 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.SingleSceneManager.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 scene1SceneManager.GetActiveScene();//场景名称Debug.Log(scene1.name);//场景路径Debug.Log(scene1.path); //场景索引Debug.Log(scene1.buildIndex); GameObject[] gb1scene1.GetRootGameObjects();Debug.Log(gb1.Length);//跳转场景//SceneManager.LoadScene(2);//SceneManager.LoadScene(Scene2);//调用异步的Start1Start1();//统计已经加载的场景数量Debug.Log(SceneManager.sceneCount);//创建新场景直接用代码Scene scene3SceneManager.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 scene2Scene scene2SceneManager.GetActiveScene();//场景是否已经加载, 但是可能还没有激活新的SceneDebug.Log(scene2.isLoaded); ///再次查看当前Scene//场景名称Debug.Log(scene2.name);//场景路径Debug.Log(scene2.path); //场景索引Debug.Log(scene2.buildIndex); GameObject[] gb2scene2.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 scene1SceneManager.GetActiveScene();//场景名称Debug.Log(scene1.name);//场景路径Debug.Log(scene1.path); //场景索引Debug.Log(scene1.buildIndex); GameObject[] gb1scene1.GetRootGameObjects();Debug.Log(gb1.Length);//跳转场景//SceneManager.LoadScene(2);//SceneManager.LoadScene(Scene2);//调用异步的Start1Start1();//统计已经加载的场景数量Debug.Log(SceneManager.sceneCount);//创建新场景直接用代码Scene scene3SceneManager.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 scene2Scene scene2SceneManager.GetActiveScene();//场景是否已经加载, 但是可能还没有激活新的SceneDebug.Log(scene2.isLoaded); ///再次查看当前Scene//场景名称Debug.Log(scene2.name);//场景路径Debug.Log(scene2.path); //场景索引Debug.Log(scene2.buildIndex); GameObject[] gb2scene2.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 operation1SceneManager.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 operation1SceneManager.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 operation1SceneManager.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()有返回值返回值类型AsyncOperationoperation1SceneManager.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 timer10;// Start is called before the first frame updatevoid Start(){//协程不能直接LoadScene(),必须新建一个协程方法StartCoroutineStartCoroutine(loadScene());}//以协程的方法来异步加载场景//必须单独写一个 loadScene()方法而不能用Application.LoadScene()方法//这个异步的方法有返回值返回值是固定的IEnumeratorIEnumerator loadScene(){// 异步的协程// SceneManager.LoadSceneAsync()有返回值返回值类型AsyncOperationoperation1SceneManager.LoadSceneAsync(2);operation1.allowSceneActivationfalse;yield return operation1;}// Update is called once per framevoid Update(){Debug.Log(operation1.progress);timer1timer1Time.deltaTime;if (timer110){operation1.allowSceneActivationtrue;}}
}