事件委托

GameManger(空物体)+GameManger脚本——重要的方式

public class GameManger : MonoBehaviour {

    public void OnStartGame(string sceneName)
{
Application.LoadLevel(sceneName);
} }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; public class GameManger : MonoBehaviour
{
public void OnStartGame(int sceneIndex)
{
//Application.LoadLevel(sceneIndex);
SceneManager.LoadScene();
}
}

Unity3D  SceneManager场景管理用法总结

一、Unity关卡

Unity 使用过程中关卡加载和卸载是大多数三维引擎都要提供的基本功能。 
因为关卡切换在游戏中非常常用。 
在之前的版本中Unity的关卡切换使用的是:

Application.loadedLevel();
  • 1看看Application类,此时这个类的功能比较繁杂,比较多。只看与关卡相关的:
  [Obsolete("Use SceneManager.LoadScene")]
public static void LoadLevel(string name); [Obsolete("Use SceneManager.LoadScene")]
public static void LoadLevel(int index); [Obsolete("Use SceneManager.LoadScene")]
public static void LoadLevelAdditive(string name); [Obsolete("Use SceneManager.LoadScene")]
public static void LoadLevelAdditive(int index);
//
// 摘要:
// ///
// Unloads all GameObject associated with the given scene. Note that assets are
// currently not unloaded, in order to free up asset memory call Resources.UnloadAllUnusedAssets.
// ///
//
// 参数:
// index:
// Index of the scene in the PlayerSettings to unload.
//
// scenePath:
// Name of the scene to Unload.
//
// 返回结果:
// ///
// Return true if the scene is unloaded.
// ///
[Obsolete("Use SceneManager.UnloadScene")]
public static bool UnloadLevel(string scenePath);
//
// 摘要:
// ///
// Unloads all GameObject associated with the given scene. Note that assets are
// currently not unloaded, in order to free up asset memory call Resources.UnloadAllUnusedAssets.
// ///
//
// 参数:
// index:
// Index of the scene in the PlayerSettings to unload.
//
// scenePath:
// Name of the scene to Unload.
//
// 返回结果:
// ///
// Return true if the scene is unloaded.
// ///
[Obsolete("Use SceneManager.UnloadScene")]
public static bool UnloadLevel(int index);

注解: 
这是之前的Application中的关卡的加载和卸载。 
当然现在在新版本(Unity5.3以上)中,有了新的变化,那就是SceneManager类了处理。

二、Untiy的SceneManager类

自从Unity5.3版本,Unity 的关卡切换就添加了新的SceneManager的类来处理。 
当然要安装过了Unity文档帮助,并且给下面路径一样,就可以知道在本地打开。 
本地链接: 
file:///C:/Program%20Files/Unity5.3.0/Editor/Data/Documentation/en/Manual/UpgradeGuide53.html 
也可以在Unity中搜索SceneManager来查看。

 #region 程序集 UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// H:\Unity\UnityProject\ShiftLevels\Library\UnityAssemblies\UnityEngine.dll
#endregion using UnityEngine.Internal; namespace UnityEngine.SceneManagement
{
//
// 摘要:
// ///
// Scene management at run-time.
// ///
public class SceneManager
{
public SceneManager(); public static int sceneCount { get; }
// public static int sceneCountInBuildSettings { get; } public static Scene GetActiveScene(); public static Scene[] GetAllScenes();
// 参数:
// index:
// Index of the scene to get. Index must be greater than or equal to 0 and less
// than SceneManager.sceneCount.
public static Scene GetSceneAt(int index); // 返回结果:
// ///
// The scene if found or an invalid scene if not.
// ///
public static Scene GetSceneByName(string name); // Searches all scenes added to the SceneManager for a scene that has the given
// asset path.
// ///
//
// 参数:
// scenePath:
// Path of the scene. Should be relative to the project folder. Like: "AssetsMyScenesMyScene.unity".
public static Scene GetSceneByPath(string scenePath);
[ExcludeFromDocs]
public static void LoadScene(int sceneBuildIndex);
[ExcludeFromDocs]
public static void LoadScene(string sceneName); // 参数:
// sceneName:
// Name of the scene to load.
//
// sceneBuildIndex:
// Index of the scene in the Build Settings to load.
//
// mode:
// Allows you to specify whether or not to load the scene additively. See SceneManagement.LoadSceneMode
// for more information about the options.
public static void LoadScene(int sceneBuildIndex, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode); // 参数:
// sceneName:
// Name of the scene to load.
//
// sceneBuildIndex:
// Index of the scene in the Build Settings to load.
//
// mode:
// Allows you to specify whether or not to load the scene additively. See SceneManagement.LoadSceneMode
// for more information about the options.
public static void LoadScene(string sceneName, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode);
[ExcludeFromDocs]
public static AsyncOperation LoadSceneAsync(int sceneBuildIndex);
[ExcludeFromDocs]
public static AsyncOperation LoadSceneAsync(string sceneName); // 参数:
// sceneName:
// Name of the scene to load.
//
// sceneBuildIndex:
// Index of the scene in the Build Settings to load.
//
// mode:
// If LoadSceneMode.Single then all current scenes will be unloaded before loading.
public static AsyncOperation LoadSceneAsync(int sceneBuildIndex, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode); // 参数:
// sceneName:
// Name of the scene to load.
//
// sceneBuildIndex:
// Index of the scene in the Build Settings to load.
//
// mode:
// If LoadSceneMode.Single then all current scenes will be unloaded before loading.
public static AsyncOperation LoadSceneAsync(string sceneName, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode);
// // 参数:
// sourceScene:
// The scene that will be merged into the destination scene.
//
// destinationScene:
// Existing scene to merge the source scene into.
public static void MergeScenes(Scene sourceScene, Scene destinationScene);
//
// 摘要:
// ///
// Move a GameObject from its current scene to a new scene. /// It is required that
// the GameObject is at the root of its current scene.
// ///
//
// 参数:
// go:
// GameObject to move.
//
// scene:
// Scene to move into.
public static void MoveGameObjectToScene(GameObject go, Scene scene);
// // 返回结果:
// ///
// Returns false if the scene is not loaded yet.
// ///
public static bool SetActiveScene(Scene scene); // ///
public static bool UnloadScene(string sceneName);
//
// 摘要:
// ///
// Unloads all GameObjects associated with the given scene. Note that assets are
// currently not unloaded, in order to free up asset memory call Resources.UnloadAllUnusedAssets.
// ///
//
// 参数:
// sceneBuildIndex:
// Index of the scene in the Build Settings to unload.
//
// sceneName:
// Name of the scene to unload.
//
// 返回结果:
// ///
// Returns true if the scene is unloaded.
// ///
public static bool UnloadScene(int sceneBuildIndex);
}
}

三、SceneManager对于获取场景的一些操作

(一) 
SceneManager 
class in UnityEngine.SceneManagement 
描述:运行时的场景管理。 
静态变量sceneCount: 当前加载的场景的总数。 
前加载的场景的数量将被返回。 
sceneCountInBuildSettings : 在BuildSettings的号码。

(二) 
CreateScene:在运行时创建一个空的新场景,使用给定的名称。 
在运行时创建一个空的新场景,使用给定的名称。 
新的场景将开放相加到层次与现有已经打开的场景。新场景的路径将是空的。此函数用于在运行时创建场景。创建一个场景编辑时间(例如,使编辑脚本或工具需要创建场景时),使用editorscenemanager.newscene。

(三) 
public static SceneManagement.Scene GetActiveScene() 
现场的活动场景。 
描述:获取当前活动场景。 
当前活动的场景将被用来作为目标由脚本实例化新的游戏对象现场。

 using UnityEngine;
using UnityEngine.SceneManagement; public class GetActiveSceneExample : MonoBehaviour
{
void Start()
{
Scene scene = SceneManager.GetActiveScene(); Debug.Log("Active scene is '" + scene.name + "'.");
}
}

(四) 
public static SceneManagement.Scene GetSceneAt(int index); 
index:场景索引。指数必须大于或等于0和小于scenemanager.scenecount。

返回: 
根据给定的参数返回一个场景引用。 
获取现场在添加场景的场景管理器的列表索引:

using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using UnityEngine;
public class Example
{
// adds a menu item which gives a brief summary of currently open scenes
[MenuItem("SceneExample/Scene Summary")]
public static void ListSceneNames()
{
string output = "";
if (SceneManager.sceneCount > )
{
for (int n = ; n < SceneManager.sceneCount; ++n)
{
Scene scene = SceneManager.GetSceneAt(n);
output += scene.name;
output += scene.isLoaded ? " (Loaded, " : " (Not Loaded, ";
output += scene.isDirty ? "Dirty, " : "Clean, ";
output += scene.buildIndex >= ? " in build)\n" : " NOT in build)\n";
}
}
else
{
output = "No open scenes.";
}
EditorUtility.DisplayDialog("Scene Summary",output, "Ok");
}
}

(五)

public static SceneManagement.Scene GetActiveScene(); 
获取当前活动场景。 
当前活动的场景将被用来作为目标由脚本实例化新对象。

 using UnityEngine;
using UnityEngine.SceneManagement; public class GetActiveSceneExample : MonoBehaviour
{
void Start()
{
Scene scene = SceneManager.GetActiveScene(); Debug.Log("Active scene is '" + scene.name + "'.");
}
} public static void LoadScene(int sceneBuildIndex, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);
public static void LoadScene(string sceneName, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);

sceneName: 需发加载的场景名称或路径。

sceneBuildIndex:在“ Build Settings”加载中的场景的索引。

Mode:允许您指定是否要加载相加现场。见LoadScene模式有关选项的详细信息。 
LoadSceneMode:在播放器加载一个场景时使用。 
Single:关闭所有当前场景和加载一个新场景。 
Additive:将场景添加到当前加载的场景中。 
你可以使用这个异步版本:LoadSceneAsync。

 using UnityEngine;
using UnityEngine.SceneManagement; public class ExampleClass : MonoBehaviour {
void Start () {
// Only specifying the sceneName or sceneBuildIndex will load the scene with the Single mode
SceneManager.LoadScene ("OtherSceneName", LoadSceneMode.Additive);
}
}

四、5.3的实现代码

上代码:

 /**************************************************************************
Copyright:@maxdong
Author: maxdong
Date: 2017-07-04
Description:加载关卡,可以分组加载和卸载。使用Unity版本为5.3.0.
因为里面使用了场景管理的一个类,这个类在5.3.0以上版本才添加的。
测试操作:使用空格键来切换场景,然后间隔5秒后才开始卸载。
**************************************************************************/
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement; [System.Serializable]
public class LevelOrder
{ [Header("每组关卡名称")]
public string[] LevelNames;
} public class ChangLevelsHasMain : MonoBehaviour
{
[Header("所有关卡列表")]
public LevelOrder[] levelOrder;
private static int index;
private int totalLevels = ;
private int levelOrderLength; void Start ()
{
for (int i = ; i < levelOrder.Length; i++)
{
totalLevels += levelOrder[i].LevelNames.Length;
} if (totalLevels != SceneManager.sceneCountInBuildSettings)
{ }
levelOrderLength = levelOrder.Length;
} // Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
bool isOk = LoadNextLevels();
if (isOk)
{
InvokeRepeating("UnloadLastLevel", 2.0f, );
}
}
} bool LoadNextLevels()
{
bool bResult = true;
//index = index % levelOrderLength;
if (index < || index >= levelOrderLength)
{
bResult = false;
return bResult;
} int LoadTimes = levelOrder[index].LevelNames.Length;
for (int i = ; i < LoadTimes; i++)
{
SceneManager.LoadSceneAsync(levelOrder[index].LevelNames[i], LoadSceneMode.Additive);
}
return bResult;
} void UnloadLastLevel()
{
if (index == )
{
index++;
CancelInvoke("UnloadLastLevel");
return;
}
// 上一組的關卡
int TmpLast = (index - ) >= ? (index - ) : levelOrderLength - ;
int LoadTimes = levelOrder[index].LevelNames.Length;
for (int i = ; i < LoadTimes; i++)
{
Scene Tmp = SceneManager.GetSceneByName(levelOrder[index].LevelNames[i]);
if (!Tmp.isLoaded)
{
return;
}
} // 下一關卡全部加載完畢後,卸載之前關卡
for (int i = ; i < levelOrder[TmpLast].LevelNames.Length; i++)
{
SceneManager.UnloadScene(levelOrder[TmpLast].LevelNames[i]);
}
index++;
CancelInvoke("UnloadLastLevel");
}
}

就这样就可以了。 
代码主要是按组来加载关卡,然后按组来卸载。 
测试中,按下空格键来加载,每组关卡在一定时间后,(这里设置的5秒)自动卸载前一组关卡。这里主地图是不卸载的,会一直存在的。

怎么设置的呢?首先需要在Build setting中中把所有要处理的关卡放进来。要不就会在加载过程中报错。 
如下图: 

然后把代码挂在主地图的任意对象对象上就可以了. 

UGUI_游戏菜单场景切换的更多相关文章

  1. cocos2dx3.1从零学习(二)菜单、场景切换、场景传值

    转:http://www.it165.net/pro/html/201406/16195.html 回顾一下上一篇的内容,我们已经学会了创建一个新的场景scene,添加sprite和label到层中, ...

  2. cocos2dx 3.1从零学习(二)——菜单、场景切换、场景传值

    回想一下上一篇的内容,我们已经学会了创建一个新的场景scene,加入sprite和label到层中.掌握了定时事件schedule. 我们能够顺利的写出打飞机的主场景框架. 上一篇的内容我练习了七个新 ...

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

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

  4. Cocos2d—X游戏开发之CCToggle(菜单标签切换)CCControlSwitch(开关切换)

    Cocos2d—X游戏开发之CCToggle(菜单标签切换) 首先继承子CCMenu,是菜单标签中的一种.‘ class CC_DLL CCMenuItemToggle : public CCMenu ...

  5. cocos2dx进阶学习之场景切换

    背景 在学习马里奥时,我们学习到从菜单场景到游戏场景的切换,代码如下 void CMMenuScene::OnStartCallBack( CCObject *pSender ) { CCDirect ...

  6. cocos2d-x开发记录:二,基本概念(导演,场景,层和精灵,场景切换,效果)

    四,Director Scene Layer和Sprite(导演,场景,层和精灵) 1.Scenes(场景) 一个场景 (用CCScene对象实现)相当于APP工作流的独立部分.一些人也喜欢叫做“屏幕 ...

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

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

  8. [Unity3D]NGUI用Sprite动画和屏幕自适应做游戏开始场景

    我们在玩任何一款手游产品时,都是先上来个logo界面,游戏欢迎界面等,这就意味着我们要做一款游戏需要多个场景,场景之间来回切换实现游戏逻辑,unity也不例外,所以从本篇开始将会介绍如何搭建多个场景, ...

  9. 【Unity3d游戏开发】UGUI插件入门之游戏菜单

    ugui是unity4.6开始加入的一个新的ui系统,非常强大,下面我们将通过一系列博客的方式一起来学习一下ugui的使用.本篇博客会介绍如何使用ugui制作一个游戏菜单,并且了解如何让物体与ugui ...

随机推荐

  1. ZooKeeper系列(一)—— ZooKeeper 简介及核心概念

    一.Zookeeper简介 Zookeeper 是一个开源的分布式协调服务,目前由 Apache 进行维护.Zookeeper 可以用于实现分布式系统中常见的发布/订阅.负载均衡.命令服务.分布式协调 ...

  2. 信安周报-第02周:SQL基础

    信安之路 第02周 Code:https://github.com/lotapp/BaseCode/tree/master/safe 前言 本周需要自行研究学习的任务贴一下: 1.概念(推荐) 数据库 ...

  3. 在vps上安装 kali linux

    在渗透测试过程中,很多时候我们需要反弹一个shell回来.使用empire也好,MSF也好,其他工具也好,都避不开公网IP的问题.这时候我们就需要一个VPS来进一步进行渗透测试. 建立通道连接的方式有 ...

  4. VS引用文件出现黄色感叹号丢失文件,应该如何解决?

    VS是微软开发的一款超级强大的IDE,深受广大.net开发者喜爱. 但是再强大,也会有它的bug和缺点. 多人协同开发时,不知道你有没有遇到一个这样的情况:第二天上班,早早来到公司,打开电脑,拉取一下 ...

  5. Okhttp3源码解析(3)-Call分析(整体流程)

    ### 前言 前面我们讲了 [Okhttp的基本用法](https://www.jianshu.com/p/8e404d9c160f) [Okhttp3源码解析(1)-OkHttpClient分析]( ...

  6. 纯数据结构Java实现(6/11)(二叉堆&优先队列)

    堆其实也是树结构(或者说基于树结构),一般可以用堆实现优先队列. 二叉堆 堆可以用于实现其他高层数据结构,比如优先队列 而要实现一个堆,可以借助二叉树,其实现称为: 二叉堆 (使用二叉树表示的堆). ...

  7. Python中模块与包的导入(朴实易懂版的总结)

    这几天,被python包与模块的导入问题,折磨的不行,以前想的很简单,其实不然,经查资料研究,特总结如下: 基本注意点 模块:一般指一个py文件:包:含有许多py文件的文件夹,含有 或不含有(Pyth ...

  8. 单元测试之NUnit三

    NUnit 分三篇文章介绍,入门者可阅读文章,有基础者直接参考官方文档.初次写博客,望大家指点. 导航: 单元测试之NUnit一 单元测试之NUnit二 单元测试之NUnit三 除了Assert断言外 ...

  9. cf 1102 B

    题意:求字符串中任意相邻两位是否可以可以由前一个加上任意个x或y屏蔽十位与后一位相等,如果可以需要添加的最少数字是多少,x值为0-9,y值也为0-9,求出任意x,y对应情形下字符串需要添加的最少数字, ...

  10. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...