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 ...
随机推荐
- RuntimeError: Model class user.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
Django 2.x版本迁移数据库报这个错误,user表使用的Django的验证系统 本来就想改一下用户表的表名,莫名的报了个这个错误,在网上找到了解决办法 打开user应用模块下的apps.py文件 ...
- win10自带虚拟机的使用(Hyper-v)
昨天刚发现的觉得特别好用,故推荐一下,跟VM虚拟机的使用方法是一样的 1.点击开始菜单中的<设置>图标,进入设置页码 2.点击<应用>图标,进入应用页码,并找到程序和功能 3. ...
- GitHub从小白到熟悉<六>
复制或 克隆 项目
- python基础之函数当中的装饰器
在实际工作当中存在一个开放封闭原则 1.对扩展是开放的 为什么要对扩展开放呢? 我们说,任何一个程序,不可能在设计之初就已经想好了所有的功能并且未来不做任何更新和修改.所以我们必须允许代码扩展.添加新 ...
- django-Auth模块(详细介绍)
转载自https://www.cnblogs.com/liuqingzheng/articles/9628105.html 1 Auth模块是什么 Auth模块是Django自带的用户认证模块: 我们 ...
- Mysql学习(三)之数据库管理工具Navicat
前言 mysql安装完后默认只有命令行工具,所以我们可以下载一些数据库管理工具Navicat Navicat使用 首先建立一个连接选择mysql,填写信息 发现多了一个localhost,双击,打开连 ...
- 107、如何配置 Health Check ? (Swarm14)
参考https://www.cnblogs.com/CloudMan6/p/8053323.html 容器状态是UP的,那应用就是健康的吗? 不一定 Docker 只能从容器启动进程的返 ...
- wex5 页面跳转
页面交互: 3种方法: 1.使用Shell提供的方法 打开另一个页面不需要等待页面返回 功能树上打开 2. 用windowDialog组件 需要等待页面返回 3.内嵌页 windowContainer ...
- JS常见面试题总结-真实被问到的!
1.判断数据类型有几种方法 console.log(typeof 'abc') // string console.log(Object.prototype.toString.call('abc')) ...
- Vue初始化一个项目
@1 npm install -g @vue/cli-init vue init webpack projectName @2 cnpm install -g @vue/cli vue create ...