1. 程式人生 > >UGUI_遊戲菜單場景切換

UGUI_遊戲菜單場景切換

幫助 result 變化 source cts void csdn BE ole

事件委托

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 使用過程中關卡加載和卸載是大多數三維引擎都要提供的基本功能。
因為關卡切換在遊戲中非常常用。
在之前的版本中Unity的關卡切換使用的是:

Application.loadedLevel();
  • 1看看Application類,此時這個類的功能比較繁雜,比較多。只看與關卡相關的:
 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類了處理。

二、Untiy的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對於獲取場景的一些操作

(一)
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 }

四、5.3的實現代碼

上代碼:

  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中中把所有要處理的關卡放進來。要不就會在加載過程中報錯。
如下圖:
技術分享圖片

然後把代碼掛在主地圖的任意對象對象上就可以了.
技術分享圖片

UGUI_遊戲菜單場景切換