一、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);
[Obsolete("Use SceneManager.UnloadScene")]
public static bool UnloadLevel(string scenePath);
[Obsolete("Use SceneManager.UnloadScene")]
public static bool UnloadLevel(int index);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
这是之前的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
#endregion
using UnityEngine.Internal;
namespace UnityEngine.SceneManagement
{
public class SceneManager
{
public SceneManager();
public static int sceneCount { get; }
public static int sceneCountInBuildSettings { get; }
public static Scene GetActiveScene();
public static Scene[] GetAllScenes();
public static Scene GetSceneAt(int index);
public static Scene GetSceneByName(string name);
public static Scene GetSceneByPath(string scenePath);
[ExcludeFromDocs]
public static void LoadScene(int sceneBuildIndex);
[ExcludeFromDocs]
public static void LoadScene(string sceneName);
public static void LoadScene(int sceneBuildIndex, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode);
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);
public static AsyncOperation LoadSceneAsync(int sceneBuildIndex, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode);
public static AsyncOperation LoadSceneAsync(string sceneName, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode);
public static void MergeScenes(Scene sourceScene, Scene destinationScene);
public static void MoveGameObjectToScene(GameObject go, Scene scene);
public static bool SetActiveScene(Scene scene);
public static bool UnloadScene(string sceneName);
public static bool UnloadScene(int sceneBuildIndex);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
注意的是这里面还有可以带对象来在关卡中移动的,还有穿越功能啊!!哈哈
三、5.3的实现代码
上代码:
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;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
bool isOk = LoadNextLevels();
if (isOk)
{
InvokeRepeating("UnloadLastLevel", 2.0f, 5);
}
}
}
bool LoadNextLevels()
{
bool bResult = true;
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");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
就这样就可以了。
代码主要是按组来加载关卡,然后按组来卸载。
测试中,按下空格键来加载,每组关卡在一定时间后,(这里设置的5秒)自动卸载前一组关卡。这里主地图是不卸载的,会一直存在的。
怎么设置的呢?首先需要在Build setting中中把所有要处理的关卡放进来。要不就会在加载过程中报错。
如下图:
然后把代码挂在主地图的任意对象对象上就可以了。
四、测试结果
随意做了几张地图,不那么好看。但是功能很明显。
第一组:
第二组
第三组