Unity3D_(API)场景切换SceneManager
2024-09-03 11:57:40
Unity场景切换SceneManager 官方文档:传送门
静态方法
创建场景
CreateScene Create an empty new Scene at runtime with the given name.
得到当前激活的场景
GetActiveScene Gets the currently active Scene.
GetActiveScene Gets the currently active Scene.
根据index得到一个场景
GetSceneAt Get the Scene at index in the SceneManager's list of loaded Scenes.
GetSceneAt Get the Scene at index in the SceneManager's list of loaded Scenes.
通过BuildIndex得到一个场景
GetSceneByBuildIndex Get a Scene struct from a build index.
通过名字得到一个场景
GetSceneByName Searches through the Scenes loaded for a Scene with the given name.
GetSceneByName Searches through the Scenes loaded for a Scene with the given name.
通过路径得到一个场景
GetSceneByPath Searches all Scenes loaded for a Scene that has the given asset path.
GetSceneByPath Searches all Scenes loaded for a Scene that has the given asset path.
加载场景
LoadScene Loads the Scene by its name or index in Build Settings.
LoadScene Loads the Scene by its name or index in Build Settings.
异步加载场景
LoadSceneAsync Loads the Scene asynchronously in the background.
LoadSceneAsync Loads the Scene asynchronously in the background.
合并两个场景
MergeScenes This will merge the source Scene into the destinationScene.
MergeScenes This will merge the source Scene into the destinationScene.
把某个游戏物体移动到场景当中
MoveGameObjectToScene Move a GameObject from its current Scene to a new Scene.
MoveGameObjectToScene Move a GameObject from its current Scene to a new Scene.
激活某个场景
SetActiveScene Set the Scene to be active.
SetActiveScene Set the Scene to be active.
异步卸载某个场景
UnloadSceneAsync Destroys all GameObjects associated with the given Scene and removes the Scene from the SceneManager
UnloadSceneAsync Destroys all GameObjects associated with the given Scene and removes the Scene from the SceneManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; public class Scene_Gary : MonoBehaviour { // Use this for initialization
void Start () {
//获得当前场景中的数量
print(SceneManager.sceneCount);
//获得BuildSettings中的名字
print(SceneManager.sceneCountInBuildSettings); //获得当前激活场景名字
print(SceneManager.GetActiveScene().name);
print(SceneManager.GetSceneAt().name); SceneManager.activeSceneChanged += OnActiveSceneChanged;
SceneManager.sceneLoaded += OnSceneLoaded;
} void OnActiveSceneChanged(Scene a, Scene b)
{
print(a.name);
print(b.name);
} void OnSceneLoaded(Scene a ,LoadSceneMode mode)
{
print(a.name+" "+mode);
} // Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
//SceneManager.LoadScene(0);
SceneManager.LoadScene(SceneManager.GetSceneByBuildIndex().name);
}
}
}
Scene_Gary.cs
//获得当前场景中的数量
print(SceneManager.sceneCount);
//获得BuildSettings中的名字
print(SceneManager.sceneCountInBuildSettings); //获得当前激活场景名字
print(SceneManager.GetActiveScene().name);
print(SceneManager.GetSceneAt().name); SceneManager.activeSceneChanged += OnActiveSceneChanged;
SceneManager.sceneLoaded += OnSceneLoaded;
void OnActiveSceneChanged(Scene a, Scene b)
{
print(a.name);
print(b.name);
} void OnSceneLoaded(Scene a ,LoadSceneMode mode)
{
print(a.name+" "+mode);
}
//重新加载场景
//SceneManager.LoadScene(0);
SceneManager.LoadScene(SceneManager.GetSceneByBuildIndex().name);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; public class Scene_Gary : MonoBehaviour { // Use this for initialization
void Start () {
SceneManager.sceneLoaded += this.OnSceneLoader;
} //当场景加载出来调用触发的事件
void OnSceneLoader(Scene scene,LoadSceneMode mode)
{
Debug.Log("Hello Gary~");
} // Update is called once per frame
void Update () {
//加载场景
SceneManager.LoadScene("Gary2");
//获得当前场景
Scene scene = SceneManager.GetActiveScene();
//重新加载当前场景
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
Scene_Gary.cs
void Start () {
//将OnSceneLoader()方法添加到场景管理类
SceneManager.sceneLoaded += this.OnSceneLoader;
} //当场景加载出来调用触发的事件
void OnSceneLoader(Scene scene,LoadSceneMode mode)
{
Debug.Log("Hello Gary~");
}
CreateScene | Create an empty new Scene at runtime with the given name. |
GetActiveScene | Gets the currently active Scene. |
GetSceneAt | Get the Scene at index in the SceneManager's list of loaded Scenes. |
GetSceneByBuildIndex | Get a Scene struct from a build index. |
GetSceneByName | Searches through the Scenes loaded for a Scene with the given name. |
GetSceneByPath | Searches all Scenes loaded for a Scene that has the given asset path. |
LoadScene | Loads the Scene by its name or index in Build Settings. |
LoadSceneAsync | Loads the Scene asynchronously in the background. |
MergeScenes | This will merge the source Scene into the destinationScene. |
MoveGameObjectToScene | Move a GameObject from its current Scene to a new Scene. |
SetActiveScene | Set the Scene to be active. |
UnloadSceneAsync | Destroys all GameObjects associated with the given Scene and removes the Scene from the SceneManager. |
场景构建
File->Build Settings
将场景放到Scenes In Build中,默认第一个场景index下标是0
按下空格,加载下标为0的场景(Gary.scene)
通过OnSceneLoader()方法,触发加载场景触发的事件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; public class Scene_Gary : MonoBehaviour { // Use this for initialization
void Start () {
//将OnSceneLoader()方法添加到场景管理类
SceneManager.sceneLoaded += this.OnSceneLoader;
} //当场景加载出来调用触发的事件
void OnSceneLoader(Scene scene,LoadSceneMode mode)
{
Debug.Log("Hello Gary~");
} // Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene();
}
}
}
Scene_Gary.cs
最新文章
- MySQL中interactive_timeout和wait_timeout的区别
- 【转载】--仅用 []()+! 就足以实现几乎任意Javascript代码
- C 语言学习 第一次作业总结
- NMAKE:fatal error U1077.“\..\.cl.exe” return code 0xc0000135
- 【转】PHP date(";Y-m-d H:i:s";);获取当前时间 差8小时解决办法
- 【codevs1409】 拦截导弹 2
- C#泛型的性能优势
- java在windows下加载dll
- MVC使用RDL报表
- mysql主从同步(3)-percona-toolkit工具(数据一致性监测、延迟监控)使用梳理
- 两个css之间的切换
- [机器学习入门篇]-Logistic函数与Softmax函数
- java 中 热部署与卸载关系
- python文件派生
- mac 安装geckodriver和chromedriver
- djangorestful framework (三)学习
- python是c语言开发的
- 单例模式(Singleton)
- Spinnaker 介绍
- 网站架构:PHP针对并发访问如何优化?
热门文章
- table表格的无缝循环
- 111、什么是stack (Swarm18)
- MySQL中自定义排序
- 禁止antd Input.Password浏览器自动回传
- 使用CXF开发WebService程序的总结(七):Spring+CXF+Mybatis+Mysql共同打造的服务端示例
- shell 脚本中的入参获取与判断
- centos 7 安装 Oracle 12c
- 安装Maatwebsite \ EXCEL \ ExcelServiceProvider
- linux服务器硬件信息查看
- Linux SUID SGID SBIT 简介和权限设定