Unity5的关卡切换
本文章由cartzhang编写,转载请注明出处。 全部权利保留。
文章链接:http://blog.csdn.net/cartzhang/article/details/51055584
作者:cartzhang
一、Unity关卡
Unity 使用过程中关卡载入和卸载是大多数三维引擎都要提供的基本功能。
由于关卡切换在游戏中很经常使用。
在之前的版本号中Unity的关卡切换使用的是:
Application.loadedLevel()
看看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);
}
}
注意的是这里面还有能够带对象来在关卡中移动的,还有穿越功能啊!
!哈哈
三、5.3的实现代码
上代码:
/**************************************************************************
Copyright:@cartzhang
Author: cartzhang
Date: 2016-04-01
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 = 0;
private int levelOrderLength;
void Start ()
{
for (int i = 0; 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, 5);
}
}
}
bool LoadNextLevels()
{
bool bResult = true;
//index = index % levelOrderLength;
if (index < 0 || index >= levelOrderLength)
{
bResult = false;
return bResult;
}
int LoadTimes = levelOrder[index].LevelNames.Length;
for (int i = 0; i < LoadTimes; i++)
{
SceneManager.LoadSceneAsync(levelOrder[index].LevelNames[i], LoadSceneMode.Additive);
}
return bResult;
}
void UnloadLastLevel()
{
if (index == 0)
{
index++;
CancelInvoke("UnloadLastLevel");
return;
}
// 上一組的關卡
int TmpLast = (index - 1) >= 0 ?
(index - 1) : levelOrderLength - 1;
int LoadTimes = levelOrder[index].LevelNames.Length;
for (int i = 0; i < LoadTimes; i++)
{
Scene Tmp = SceneManager.GetSceneByName(levelOrder[index].LevelNames[i]);
if (!Tmp.isLoaded)
{
return;
}
}
// 下一關卡全部加載完畢後。卸載之前關卡
for (int i = 0; i < levelOrder[TmpLast].LevelNames.Length; i++)
{
SceneManager.UnloadScene(levelOrder[TmpLast].LevelNames[i]);
}
index++;
CancelInvoke("UnloadLastLevel");
}
}
就这样就能够了。
代码主要是按组来载入关卡。然后按组来卸载。
測试中,按下空格键来载入。每组关卡在一定时间后,(这里设置的5秒)自己主动卸载前一组关卡。这里主地图是不卸载的,会一直存在的。
怎么设置的呢?首先须要在Build setting中中把全部要处理的关卡放进来。要不就会在载入过程中报错。
例如以下图:
然后把代码挂在主地图的随意对象对象上就能够了。
四、測试结果
随意做了几张地图。不那么好看。
可是功能很明显。
第一组:
第二组
第三组
參考
file:///C:/Program%20Files/Unity5.3.0/Editor/Data/Documentation/en/Manual/UpgradeGuide53.html
追加Github地址:https://github.com/cartzhang/ShiftLevels
————————–THE———END————–
若有问题。请随时联系!。
很感谢!!
你在桥上看风景。我在楼里加班!
Unity5的关卡切换的更多相关文章
- UE4 difference between servertravel and openlevel(多人游戏的关卡切换)
多人游戏的关卡切换分为无缝和非无缝.非无缝切换时,客户端将跟服务器断开连接,然后重新连接到同一个服务器,服务器则加载一个新地图.无缝切换不会发生这样的情况. 有三个函数供我们使用:UEngine::B ...
- 【UE4 C++】关卡切换、流关卡加载卸载
切换关卡 基于 UGameplayStatics:: OenLevel UGameplayStatics::OpenLevel(GetWorld(), TEXT("NewMap") ...
- UGUI_游戏菜单场景切换
事件委托 GameManger(空物体)+GameManger脚本——重要的方式 public class GameManger : MonoBehaviour { public void OnSta ...
- Unity AssetBundles and Resources指引 (四) AssetBundle使用模式
本文内容主要翻译自下面这篇文章 https://unity3d.com/cn/learn/tutorials/topics/best-practices/guide-assetbundles-and- ...
- Unity项目 - MissionDemolition 愤怒的小鸟核心机制
目录 游戏原型 项目演示 绘图资源 代码实现 注意事项 技术探讨 参考来源 游戏原型 爆破任务 MissionDemolition 是一款核心机制类似于愤怒的小鸟的游戏,玩家将用弹弓发射炮弹,摧毁城堡 ...
- 《InsideUE4》-8-GamePlay架构(七)GameMode和GameState
我的世界,我做主 引言 上文我们说到在Actor层次,UE用Controller来充当APawn的逻辑控制者,也有了可以接受玩家输入的PlayerController,和能自行行动的AIControl ...
- Unity内存理解(转)
Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个通过AssetBundle,其实两者区别不大. Resources.Load就是从一个缺省打进程序包里的AssetBu ...
- 【转载】Unity 优雅地管理资源,减少占用内存,优化游戏
转自:星辰的<Unity3D占用内存太大的解决方法> 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大. 这里写下关于Unity3D对于内存的管理与优化. Unity3D ...
- Unity3D占用内存太大的解决方法
原地址:http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大 ...
随机推荐
- Node.js fs文件系统模块
一.读取文件几个步骤 1.先引入fs文件系统模块 2.使用readfile 或 readFileSync 注意点:它们的回调函数有两个参数,第一个是err,第二个是data,得到的data是buffe ...
- ABAQUS学习记录1——用户子程序综述
概述 ABAQUS提供了相当丰富的单元类型,材料属性等数据库可供用户选择,但是工程问题是千变万化的,为了满足用户的特殊工程要求,ABAQUS为用户提供了强大而又灵活的用户子程序接口(USER SUBR ...
- Yii1 用commandBuilder方法往数据表中插入多条记录
$builder = Yii::app()->db->schema->commandBuilder; // 创建builder对象 $command = $builder->c ...
- 暴力破解UltraEdit
使用x32dbg(x64dbg)做为破解工具: 使用x64dbg 打开 udeit64.exe 点击运行,直到UltraEdit启动,转到符号页: 找到: CheckForUpdatesNoPromp ...
- HDU-4791-Alice‘s Print Service
分析: 1.由于价格是递减的,所以可能出现si*pi>sj*pj(j>i).所以要有一个数组来储存当前端点的最小值. 2.然后二分查找当前的si,比较q*p[i]和M[i+1].不过在这之 ...
- 牛客网数据库SQL实战 1-11
1. 查找最晚入职员工的所有信息 CREATE TABLE `employees` ( `emp_no` ) NOT NULL, `birth_date` date NOT NULL, `first_ ...
- PHP中的正则
概述 正则表达式是一种描述字符串结果的语法规则,是一个特定的格式化模式,可以匹配.替换.截取匹配的字符串. j简单的说就是通过一些规定的符号和字符组合成的一种语法规则 其实,只有了解一种语言的正则使用 ...
- CSS3--- 颜色
1.RGB是一种色彩标准,是由红(R).绿(G).蓝(B)的变化以及相互叠加来得到各式各样的颜色.RGBA是在RGB的基础上增加了控制alpha透明度的参数. 语法:color:rgba(R,G,B, ...
- 单点登录-CAS
使用插件 phpCAS ,插件链接:https://github.com/apereo/phpCAS 1. CAS 单点登录流程及插件的API的简介 图片链接来源于wiki:https://wiki. ...
- NYOJ660逃离地球——只为最大存活率~
逃离地球 时间限制:1000 ms | 内存限制:65535 KB 难度: 描述 据霍金的<时间简史>所述,在几亿年之后将再次发生宇宙大爆炸.在宇宙大爆炸后,地球上将新生出许多生物而不 ...