Unity场景切换SceneManager  官方文档:传送门

静态方法

  创建场景

  CreateScene   Create an empty new Scene at runtime with the given name.
 
  得到当前激活的场景
  GetActiveScene   Gets the currently active Scene.
 
  根据index得到一个场景
  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.
  
  通过路径得到一个场景
  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的更多相关文章

  1. Unity 之 场景切换

      Application.LoadLevel();//场景名称或索引,删除掉原场景的所有东西 Application.LoadLevelAdditive()//添加并加载场景,不删除当前场景的物体, ...

  2. Unity 中场景切换

    Unity游戏开发中,单个Scene解决所有问题似乎不可能,那么多个Scene之间的切换是必然存在.如果仅仅是切换,似乎什么都好说,但是在场景比较大的时候不想让玩家等待加载或者说场景与场景之间想通过一 ...

  3. 动画间隔AnimationInterval 场景切换、图层叠加

    从这一个月的学习进度上来看算比较慢的了,从开始学习C++到初试cocos,这也是我做过的比较大的决定,从工作中里挤出时间来玩玩自己喜欢的游戏开发也是一件非常幸福的事情,虽然现在对cocos的了解还只是 ...

  4. unity 之 场景切换进度条显示

    一.UI.建立slider适当更改即可: 二.新增loadScene脚本,用来进行场景切换,将其绑定任意物体上面.博主以放置主相机为例.参数分别为进度条(用来设置value值),显示进度文本text: ...

  5. 【Cocos2d-x 3.x】 场景切换生命周期、背景音乐播放和场景切换原理与源码分析

    大部分游戏里有很多个场景,场景之间需要切换,有时候切换的时候会进行背景音乐的播放和停止,因此对这块内容进行了总结. 场景切换生命周期 场景切换用到的函数: bool Setting::init() { ...

  6. texturepacker打包图片,场景切换时背景图有黑边

    在使用TexturePacker打包图片之后,背景图在场景切换(有切换动画)时,明显能看到有黑边,在百度之后解决了. 知乎上边有网友贴出了两种解决方法,我抄过来如下: 第一种: 修改 ccConfig ...

  7. cocos2d-x 帧循环不严谨造成场景切换卡顿

    最近在用cocos2d-x做引导界面,2dx版本是2.2.3,场景切换加上了效果,所有资源都已经使用texturepacker打包预加载,但是在实际运行调试中,场景切换相当卡顿. 各种纠结后,无意中将 ...

  8. 自制Unity小游戏TankHero-2D(5)声音+爆炸+场景切换+武器弹药

    自制Unity小游戏TankHero-2D(5)声音+爆炸+场景切换+武器弹药 我在做这样一个坦克游戏,是仿照(http://game.kid.qq.com/a/20140221/028931.htm ...

  9. JavaScript强化教程 -- cocosjs场景切换

    场景切换 在main.js,将StartScene作为我们初始化运行的场景,代码如下: cc.LoaderScene.preload(g_resources, function () { cc.dir ...

随机推荐

  1. mongodb 数据操作(1)

    切换/创建数据库 use test 添加数据db.student.save({name:"J33ack",age:25}) 查看数据库show dbs 删除当前数据库 db.dro ...

  2. 最短路(hdu2544)

    在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助 ...

  3. volatile关键字和transient关键字

    Java 语言包含两种内在的同步机制:同步块(或方法)和 volatile 变量 1.volatile 关键字-----针对变量的可变性 专业解释:volatile关键字是个类型修饰符用它声明的类型变 ...

  4. php重定向的三种方法分享

    一.用HTTP头信息  也就是用PHP的HEADER函数.PHP里的HEADER函数的作用就是向浏览器发出由HTTP协议规定的本来应该通过WEB服务器的控制指令,例如: 声明返回信息的类型(" ...

  5. java 关于数字取小数点后两位出现整数0没有的问题

    最近再项目中对取到的一系列带很长小数的数字,展现时要求去小数点后两位显示就可以了 开始我是以下写法: double  a =  0.1234455; DecimalFormat decimalForm ...

  6. 07 Python之协程

    协程: 协程是一种用户态的轻量级线程, 即协程是由用户程序自己控制调度的 1.Greenlet import time # import greenlet from greenlet import g ...

  7. JDBC1

    ---恢复内容开始--- create table `account` ( `id` ), `name` ), `balance` ) ); insert into `account` (`id`, ...

  8. PL/SQL题型代码示例

    1.记录类型(注意标点符号的使用) 结果: 2.学习流程 3. 4. 5. 6. 写法二: 结果: 写法三: 7.使用循环语句打印1-100 方法一: 或者 方法二: 方法三: 8. 方法二: 9. ...

  9. 使用TensorFlow玩GTA5

    小白学TensorFlow(一) tensorflow安装 在安装之前,您必须选择以下类型的TensorFlow之一来安装: TensorFlow仅支持CPU支​​持.如果您的系统没有NVIDIA®G ...

  10. centos7搭建activemq服务

    一.下载安装jdk 下载 jdk-8u211-linux-x64.rpm安装: yum -y install jdk-8u211-linux-x64.rpm 二.官网下载 activemq 软件包 官 ...