标签:relative nbsp class menu mono 分组 files called ram
事件委托
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(1); } }
Unity3D SceneManager场景管理用法总结
Unity 使用过程中关卡加载和卸载是大多数三维引擎都要提供的基本功能。
因为关卡切换在游戏中非常常用。
在之前的版本中Unity的关卡切换使用的是:
Application.loadedLevel();
1 [Obsolete("Use SceneManager.LoadScene")] 2 public static void LoadLevel(string name); 3 4 [Obsolete("Use SceneManager.LoadScene")] 5 public static void LoadLevel(int index); 6 7 [Obsolete("Use SceneManager.LoadScene")] 8 public static void LoadLevelAdditive(string name); 9 10 [Obsolete("Use SceneManager.LoadScene")] 11 public static void LoadLevelAdditive(int index); 12 // 13 // 摘要: 14 // /// 15 // Unloads all GameObject associated with the given scene. Note that assets are 16 // currently not unloaded, in order to free up asset memory call Resources.UnloadAllUnusedAssets. 17 // /// 18 // 19 // 参数: 20 // index: 21 // Index of the scene in the PlayerSettings to unload. 22 // 23 // scenePath: 24 // Name of the scene to Unload. 25 // 26 // 返回结果: 27 // /// 28 // Return true if the scene is unloaded. 29 // /// 30 [Obsolete("Use SceneManager.UnloadScene")] 31 public static bool UnloadLevel(string scenePath); 32 // 33 // 摘要: 34 // /// 35 // Unloads all GameObject associated with the given scene. Note that assets are 36 // currently not unloaded, in order to free up asset memory call Resources.UnloadAllUnusedAssets. 37 // /// 38 // 39 // 参数: 40 // index: 41 // Index of the scene in the PlayerSettings to unload. 42 // 43 // scenePath: 44 // Name of the scene to Unload. 45 // 46 // 返回结果: 47 // /// 48 // Return true if the scene is unloaded. 49 // /// 50 [Obsolete("Use SceneManager.UnloadScene")] 51 public static bool UnloadLevel(int index);
注解:
这是之前的Application中的关卡的加载和卸载。
当然现在在新版本(Unity5.3以上)中,有了新的变化,那就是SceneManager类了处理。
自从Unity5.3版本,Unity 的关卡切换就添加了新的SceneManager的类来处理。
当然要安装过了Unity文档帮助,并且给下面路径一样,就可以知道在本地打开。
本地链接:
file:///C:/Program%20Files/Unity5.3.0/Editor/Data/Documentation/en/Manual/UpgradeGuide53.html
也可以在Unity中搜索SceneManager来查看。
1 #region 程序集 UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null 2 // H:\Unity\UnityProject\ShiftLevels\Library\UnityAssemblies\UnityEngine.dll 3 #endregion 4 5 using UnityEngine.Internal; 6 7 namespace UnityEngine.SceneManagement 8 { 9 // 10 // 摘要: 11 // /// 12 // Scene management at run-time. 13 // /// 14 public class SceneManager 15 { 16 public SceneManager(); 17 18 19 public static int sceneCount { get; } 20 // 21 22 public static int sceneCountInBuildSettings { get; } 23 24 25 public static Scene GetActiveScene(); 26 27 public static Scene[] GetAllScenes(); 28 // 参数: 29 // index: 30 // Index of the scene to get. Index must be greater than or equal to 0 and less 31 // than SceneManager.sceneCount. 32 public static Scene GetSceneAt(int index); 33 34 // 返回结果: 35 // /// 36 // The scene if found or an invalid scene if not. 37 // /// 38 public static Scene GetSceneByName(string name); 39 40 // Searches all scenes added to the SceneManager for a scene that has the given 41 // asset path. 42 // /// 43 // 44 // 参数: 45 // scenePath: 46 // Path of the scene. Should be relative to the project folder. Like: "AssetsMyScenesMyScene.unity". 47 public static Scene GetSceneByPath(string scenePath); 48 [ExcludeFromDocs] 49 public static void LoadScene(int sceneBuildIndex); 50 [ExcludeFromDocs] 51 public static void LoadScene(string sceneName); 52 53 // 参数: 54 // sceneName: 55 // Name of the scene to load. 56 // 57 // sceneBuildIndex: 58 // Index of the scene in the Build Settings to load. 59 // 60 // mode: 61 // Allows you to specify whether or not to load the scene additively. See SceneManagement.LoadSceneMode 62 // for more information about the options. 63 public static void LoadScene(int sceneBuildIndex, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode); 64 65 // 参数: 66 // sceneName: 67 // Name of the scene to load. 68 // 69 // sceneBuildIndex: 70 // Index of the scene in the Build Settings to load. 71 // 72 // mode: 73 // Allows you to specify whether or not to load the scene additively. See SceneManagement.LoadSceneMode 74 // for more information about the options. 75 public static void LoadScene(string sceneName, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode); 76 [ExcludeFromDocs] 77 public static AsyncOperation LoadSceneAsync(int sceneBuildIndex); 78 [ExcludeFromDocs] 79 public static AsyncOperation LoadSceneAsync(string sceneName); 80 81 // 参数: 82 // sceneName: 83 // Name of the scene to load. 84 // 85 // sceneBuildIndex: 86 // Index of the scene in the Build Settings to load. 87 // 88 // mode: 89 // If LoadSceneMode.Single then all current scenes will be unloaded before loading. 90 public static AsyncOperation LoadSceneAsync(int sceneBuildIndex, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode); 91 92 // 参数: 93 // sceneName: 94 // Name of the scene to load. 95 // 96 // sceneBuildIndex: 97 // Index of the scene in the Build Settings to load. 98 // 99 // mode: 100 // If LoadSceneMode.Single then all current scenes will be unloaded before loading. 101 public static AsyncOperation LoadSceneAsync(string sceneName, [DefaultValue("LoadSceneMode.Single")] LoadSceneMode mode); 102 // 103 104 // 参数: 105 // sourceScene: 106 // The scene that will be merged into the destination scene. 107 // 108 // destinationScene: 109 // Existing scene to merge the source scene into. 110 public static void MergeScenes(Scene sourceScene, Scene destinationScene); 111 // 112 // 摘要: 113 // /// 114 // Move a GameObject from its current scene to a new scene. /// It is required that 115 // the GameObject is at the root of its current scene. 116 // /// 117 // 118 // 参数: 119 // go: 120 // GameObject to move. 121 // 122 // scene: 123 // Scene to move into. 124 public static void MoveGameObjectToScene(GameObject go, Scene scene); 125 // 126 127 // 返回结果: 128 // /// 129 // Returns false if the scene is not loaded yet. 130 // /// 131 public static bool SetActiveScene(Scene scene); 132 133 // /// 134 public static bool UnloadScene(string sceneName); 135 // 136 // 摘要: 137 // /// 138 // Unloads all GameObjects associated with the given scene. Note that assets are 139 // currently not unloaded, in order to free up asset memory call Resources.UnloadAllUnusedAssets. 140 // /// 141 // 142 // 参数: 143 // sceneBuildIndex: 144 // Index of the scene in the Build Settings to unload. 145 // 146 // sceneName: 147 // Name of the scene to unload. 148 // 149 // 返回结果: 150 // /// 151 // Returns true if the scene is unloaded. 152 // /// 153 public static bool UnloadScene(int sceneBuildIndex); 154 } 155 }
(一)
SceneManager
class in UnityEngine.SceneManagement
描述:运行时的场景管理。
静态变量sceneCount: 当前加载的场景的总数。
前加载的场景的数量将被返回。
sceneCountInBuildSettings : 在BuildSettings的号码。
(二)
CreateScene:在运行时创建一个空的新场景,使用给定的名称。
在运行时创建一个空的新场景,使用给定的名称。
新的场景将开放相加到层次与现有已经打开的场景。新场景的路径将是空的。此函数用于在运行时创建场景。创建一个场景编辑时间(例如,使编辑脚本或工具需要创建场景时),使用editorscenemanager.newscene。
(三)
public static SceneManagement.Scene GetActiveScene()
现场的活动场景。
描述:获取当前活动场景。
当前活动的场景将被用来作为目标由脚本实例化新的游戏对象现场。
1 using UnityEngine; 2 using UnityEngine.SceneManagement; 3 4 public class GetActiveSceneExample : MonoBehaviour 5 { 6 void Start() 7 { 8 Scene scene = SceneManager.GetActiveScene(); 9 10 Debug.Log("Active scene is ‘" + scene.name + "‘."); 11 } 12 }
(四)
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 > 0) { for (int n = 0; 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 >=0 ? " in build)\n" : " NOT in build)\n"; } } else { output = "No open scenes."; } EditorUtility.DisplayDialog("Scene Summary",output, "Ok"); } }
(五)
public static SceneManagement.Scene GetActiveScene();
获取当前活动场景。
当前活动的场景将被用来作为目标由脚本实例化新对象。
1 using UnityEngine; 2 using UnityEngine.SceneManagement; 3 4 public class GetActiveSceneExample : MonoBehaviour 5 { 6 void Start() 7 { 8 Scene scene = SceneManager.GetActiveScene(); 9 10 Debug.Log("Active scene is ‘" + scene.name + "‘."); 11 } 12 } 13 14 public static void LoadScene(int sceneBuildIndex, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single); 15 public static void LoadScene(string sceneName, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);
sceneName: 需发加载的场景名称或路径。
sceneBuildIndex:在“ Build Settings”加载中的场景的索引。
Mode:允许您指定是否要加载相加现场。见LoadScene模式有关选项的详细信息。
LoadSceneMode:在播放器加载一个场景时使用。
Single:关闭所有当前场景和加载一个新场景。
Additive:将场景添加到当前加载的场景中。
你可以使用这个异步版本:LoadSceneAsync。
1 using UnityEngine; 2 using UnityEngine.SceneManagement; 3 4 public class ExampleClass : MonoBehaviour { 5 void Start () { 6 // Only specifying the sceneName or sceneBuildIndex will load the scene with the Single mode 7 SceneManager.LoadScene ("OtherSceneName", LoadSceneMode.Additive); 8 } 9 }
上代码:
1 /************************************************************************** 2 Copyright:@maxdong 3 Author: maxdong 4 Date: 2017-07-04 5 Description:加载关卡,可以分组加载和卸载。使用Unity版本为5.3.0. 6 因为里面使用了场景管理的一个类,这个类在5.3.0以上版本才添加的。 7 测试操作:使用空格键来切换场景,然后间隔5秒后才开始卸载。 8 **************************************************************************/ 9 using UnityEngine; 10 using System.Collections; 11 using UnityEngine.SceneManagement; 12 13 [System.Serializable] 14 public class LevelOrder 15 { 16 17 [Header("每组关卡名称")] 18 public string[] LevelNames; 19 } 20 21 public class ChangLevelsHasMain : MonoBehaviour 22 { 23 [Header("所有关卡列表")] 24 public LevelOrder[] levelOrder; 25 private static int index; 26 private int totalLevels = 0; 27 private int levelOrderLength; 28 29 void Start () 30 { 31 for (int i = 0; i < levelOrder.Length; i++) 32 { 33 totalLevels += levelOrder[i].LevelNames.Length; 34 } 35 36 if (totalLevels != SceneManager.sceneCountInBuildSettings) 37 { 38 39 } 40 levelOrderLength = levelOrder.Length; 41 } 42 43 // Update is called once per frame 44 void Update () 45 { 46 if (Input.GetKeyDown(KeyCode.Space)) 47 { 48 bool isOk = LoadNextLevels(); 49 if (isOk) 50 { 51 InvokeRepeating("UnloadLastLevel", 2.0f, 5); 52 } 53 } 54 } 55 56 bool LoadNextLevels() 57 { 58 bool bResult = true; 59 //index = index % levelOrderLength; 60 if (index < 0 || index >= levelOrderLength) 61 { 62 bResult = false; 63 return bResult; 64 } 65 66 int LoadTimes = levelOrder[index].LevelNames.Length; 67 for (int i = 0; i < LoadTimes; i++) 68 { 69 SceneManager.LoadSceneAsync(levelOrder[index].LevelNames[i], LoadSceneMode.Additive); 70 } 71 return bResult; 72 } 73 74 void UnloadLastLevel() 75 { 76 if (index == 0) 77 { 78 index++; 79 CancelInvoke("UnloadLastLevel"); 80 return; 81 } 82 // 上一組的關卡 83 int TmpLast = (index - 1) >= 0 ? (index - 1) : levelOrderLength - 1; 84 int LoadTimes = levelOrder[index].LevelNames.Length; 85 for (int i = 0; i < LoadTimes; i++) 86 { 87 Scene Tmp = SceneManager.GetSceneByName(levelOrder[index].LevelNames[i]); 88 if (!Tmp.isLoaded) 89 { 90 return; 91 } 92 } 93 94 // 下一關卡全部加載完畢後,卸載之前關卡 95 for (int i = 0; i < levelOrder[TmpLast].LevelNames.Length; i++) 96 { 97 SceneManager.UnloadScene(levelOrder[TmpLast].LevelNames[i]); 98 } 99 index++; 100 CancelInvoke("UnloadLastLevel"); 101 } 102 }
就这样就可以了。
代码主要是按组来加载关卡,然后按组来卸载。
测试中,按下空格键来加载,每组关卡在一定时间后,(这里设置的5秒)自动卸载前一组关卡。这里主地图是不卸载的,会一直存在的。
怎么设置的呢?首先需要在Build setting中中把所有要处理的关卡放进来。要不就会在加载过程中报错。
如下图:
然后把代码挂在主地图的任意对象对象上就可以了.
标签:relative nbsp class menu mono 分组 files called ram
原文地址:https://www.cnblogs.com/NBOWeb/p/8967519.html