Unity3D_(API)场景切换SceneManager
Unity场景切换SceneManager 官方文档:传送门
静态方法
创建场景
GetActiveScene Gets the currently active Scene.
GetSceneAt Get the Scene at index in the SceneManager's list of loaded Scenes.
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

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
Unity3D_(API)场景切换SceneManager的更多相关文章
- Unity 之 场景切换
Application.LoadLevel();//场景名称或索引,删除掉原场景的所有东西 Application.LoadLevelAdditive()//添加并加载场景,不删除当前场景的物体, ...
- Unity 中场景切换
Unity游戏开发中,单个Scene解决所有问题似乎不可能,那么多个Scene之间的切换是必然存在.如果仅仅是切换,似乎什么都好说,但是在场景比较大的时候不想让玩家等待加载或者说场景与场景之间想通过一 ...
- 动画间隔AnimationInterval 场景切换、图层叠加
从这一个月的学习进度上来看算比较慢的了,从开始学习C++到初试cocos,这也是我做过的比较大的决定,从工作中里挤出时间来玩玩自己喜欢的游戏开发也是一件非常幸福的事情,虽然现在对cocos的了解还只是 ...
- unity 之 场景切换进度条显示
一.UI.建立slider适当更改即可: 二.新增loadScene脚本,用来进行场景切换,将其绑定任意物体上面.博主以放置主相机为例.参数分别为进度条(用来设置value值),显示进度文本text: ...
- 【Cocos2d-x 3.x】 场景切换生命周期、背景音乐播放和场景切换原理与源码分析
大部分游戏里有很多个场景,场景之间需要切换,有时候切换的时候会进行背景音乐的播放和停止,因此对这块内容进行了总结. 场景切换生命周期 场景切换用到的函数: bool Setting::init() { ...
- texturepacker打包图片,场景切换时背景图有黑边
在使用TexturePacker打包图片之后,背景图在场景切换(有切换动画)时,明显能看到有黑边,在百度之后解决了. 知乎上边有网友贴出了两种解决方法,我抄过来如下: 第一种: 修改 ccConfig ...
- cocos2d-x 帧循环不严谨造成场景切换卡顿
最近在用cocos2d-x做引导界面,2dx版本是2.2.3,场景切换加上了效果,所有资源都已经使用texturepacker打包预加载,但是在实际运行调试中,场景切换相当卡顿. 各种纠结后,无意中将 ...
- 自制Unity小游戏TankHero-2D(5)声音+爆炸+场景切换+武器弹药
自制Unity小游戏TankHero-2D(5)声音+爆炸+场景切换+武器弹药 我在做这样一个坦克游戏,是仿照(http://game.kid.qq.com/a/20140221/028931.htm ...
- JavaScript强化教程 -- cocosjs场景切换
场景切换 在main.js,将StartScene作为我们初始化运行的场景,代码如下: cc.LoaderScene.preload(g_resources, function () { cc.dir ...
随机推荐
- Python 入门 之 类的三大关系(依赖 / 组合/ 继承关系)
Python 入门 之 类的三大关系(依赖 / 组合/ 继承关系) 在面向对象的中,类与类之间存在三种关系:依赖关系.组合关系.继承关系. 1.依赖关系:将一个类的类名或对象当做参数传递给另一个函数被 ...
- vscode中eslint插件的配置-prettier
用vue-cli构建vue项目,会有个eslint代码检测的安装 可vscode自带代码格式化是prettier格式(右键有格式化文件或alt+shift+f) 这时候要在vscode上装一个esli ...
- 模块之re模块 正则表达式
正则表达式,正则表达式在处理字符串上有先天的优势,尤其大数量的字符串.先来记一个网站,此网站功能就是关于正则表达式方面的应用http://tool.chinaz.com/regex/ 单纯的正则表达式 ...
- C++ 类类型转换函数explicit 关键字
标准数据之间会进行 隐式类型安全转换. 转换规则: 隐式类型转换的问题: #include <iostream> #include <string> using namesp ...
- sql--Drop语句
通过使用 DROP 语句,可以轻松地删除索引.表和数据库. SQL DROP INDEX 语句 我们可以使用 DROP INDEX 命令删除表格中的索引. 用于 Microsoft SQLJet (以 ...
- session+验证码 学习
分析 LoginServlet类 @WebServlet("/loginServlet") public class LoginServlet extends HttpServle ...
- ASCII、Unicode、UTF-8、UTF-16、GBK、GB2312、ANSI等编码方式简析
ASCII.Unicode.UTF-8.UTF-16.GBK.GB2312.ANSI等编码方式简析 序言 从各种字节编码方法中,能看到那个计算机发展的洪荒时期的影子. ASCII ASCII码有标准A ...
- 三剑客-awk(简写)
特殊要点:$0 表示整个当前行$1 每行第一个字段NF 字段数量变量NR 每行的记录号,多文件记录递增OFS 输出字段分隔符, 默认也是空格,可以改为制表符等ORS 输出的记录分隔符,默认为换行符,即 ...
- O005、远程管理 KVM 虚机
参考https://www.cnblogs.com/CloudMan6/p/5256018.html 上一节我们通过 virt-manager 在本地主机上创建并管理 KVM 虚机,其实 virt ...
- docker之常用命令
1) docker run -p : --name mysql -v d:/docker/mysql/conf:/etc/mysql/conf.d -v d:/docker/mysql/logs:/l ...