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 ...
随机推荐
- redis在php中实际应用-hash
Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象. 目录: 1.批量赋值:hmset,hmget,hgetall 可用于存储一条条数据,即一个 ...
- Python 入门 之 包
Python 入门 之 包 1.包 (1)什么是包? 文件夹下具有_ init.py _的文件夹就是一个包 (2)包的作用: 管理模块(文件化) (3)包的导入: 导入: 启动文件要和包文件是同级 绝 ...
- mysql索引失效的情况
1.WHERE字句的查询条件里有不等于号(如:WHERE column!=...),MYSQL将无法使用索引: 2.WHERE字句的查询条件里使用了函数的列(如:WHERE DAY(column)=. ...
- python网络爬虫(9)构建基础爬虫思路
目的意义 基础爬虫分5个模块,使用多个文件相互配合,实现一个相对完善的数据爬取方案,便于以后更完善的爬虫做准备. 这里目的是爬取200条百度百科信息,并生成一个html文件,存储爬取的站点,词条,解释 ...
- 雷赛DMC2410_入门篇
研究了一下雷赛的运动控制卡,还是花了一点时间,总算把步进电机转起来了,现在把整个过程分享给大家. 雷赛板卡型号很多,这里选择的是DMC2410,主要在于他的性价比,其他型号应该也差不多同样的原理,套装 ...
- 前端点击下载excel表格数据
<el-button type="primary" @click="downloadChartData" size="mini"> ...
- 正则替换replace中$1的用法
一.repalce定义 用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 1 2 3 4 5 stringObject.replace(regexp/substr,repla ...
- Hadoop网页监控配置
接之前的内容http://www.cnblogs.com/jourluohua/p/8734406.html 在之前那的内容中,仅实现了Hadoop的安装和运行,距离实际使用还有很远.现在先完成一个小 ...
- opencart nginx静态化设置
在niginx设置里添加下面代码,(lnmp的可能是 网址.conf文件添加) # SEO URL Settings # Nginx configuration of OC htaccess loca ...
- 从0开始Jmeter接口测试实战
在之前的文章中给大家介绍过接口测试文档和接口测试用例示例,本文基于Jmeter工具给大家介绍一下如何实现接口测试用例:包括发起Http请求,绕过登陆,验证响应.JMeter是Apache组织开发的基于 ...