-
引言
游戲基本架構大致分為介紹界面(introduction),主菜單界面(Menu),游戲界面(Game),設置界面(Setting),排行榜(high score)。
在很多開發引擎中都結合以上架構存在類似的概念。安卓的活動(Activity),視圖(View)等。在Unity中更是有場景(Scene)這一個概念。
Scene就是一組相關聯的游戲對象的一個集合,通常每個集合就是一個場景,但是也有可能只是一個場景的一部分!
那么怎么管理游戲場景及游戲關卡是很重要的一個問題。其中涉及到場景跳轉,場景加載等。如下介紹一款插件:Scene Manager。
提供官網地址。其中有DEMO,Tutorials,documentation,support。可供學習。
也可在unity 資源商店導入。
上該篇講述后的工程視頻:
http://v.youku.com/v_show/id_XNjY2NzA5NDY0.html
-
Unity實現過程
介紹:一個菜單界面,點擊進入游戲關卡一,點擊進入游戲關卡二,點擊回到菜單界面。
新建一個Unity工程,命名為SceneManagerTest.
保存場景為MyMainMenu.新建2個場景(游戲關卡一,二)為MyLevel1,MyLevel2.
導入插件包。其中插件包中的實例就不說了,直接進入簡單的應用開發吧。
在工程視圖中新建"Resources"、"MyScenes"、"MyScripts"、三個文件夾。
復制SceneManagerDemo中的SMGameEnvironment.cs腳本到MyScripts文件夾中,復制Transitions文件夾到新建的Resources下,刪除SceneManagerDemo.
在Resources文件夾下新建"MyConfig"文件夾,選中Assets點擊工程視圖中的Create,點擊選擇“SceneConfiguration”,在MyConfig文件夾中可以看到"SMSceneConfiguration.cs".
選中“SMSceneConfiguration”,在檢視面板中可以看到之前我們新建的三個場景,點擊Activate按鈕
在見識面板中的"AvaliableScenes"中選擇場景,點擊對應的功能按鈕(Level、Screen等)如下圖示:
在MyMainMenu場景中新建一個空物體,命名為MyMenuController,新建ShowGame.cs腳本如下:並將腳本掛在MyMenuController物體上。
1 using UnityEngine; 2 using System.Collections; 3 4 //我自定義啦。。 5 6 7 public class ShowGame : MonoBehaviour { 8 9 private SMSceneManager mSMSceneManager; 10 private SMILevelProgress mLevelProgress; 11 // Use this for initialization 12 void Start () { 13 mSMSceneManager = SMGameEnvironment.Instance.SceneManager; 14 mLevelProgress = mSMSceneManager.UnmodifiableLevelProgress; 15 } 16 17 // Update is called once per frame 18 void Update () { 19 20 } 21 22 void OnGUI(){ 23 //if(!mLevelProgress.HasPlayed){ 24 if(GUI.Button(new Rect(200,200,200,200),"MenuScene")){ 25 mSMSceneManager.LoadFirstLevel(); 26 } 27 //} 28 } 29 }
分別在MyLevel1,MyLevel2場景中新建空物體為MyLevel1Controller,MyLevel2Controller,新建ShowNextLevel.cs腳本一並掛於空物體MyLevel1Controller,MyLevel2Controller上、
1 using UnityEngine; 2 using System.Collections; 3 4 //我自定義啦。。 5 6 7 public class ShowNextLevel : MonoBehaviour { 8 9 10 // Use this for initialization 11 void Start () { 12 13 } 14 15 // Update is called once per frame 16 void Update () { 17 18 } 19 20 21 void OnGUI(){ 22 if(GUI.Button(new Rect(200,200,200,200),this.name)){ 23 SMGameEnvironment.Instance.SceneManager.LoadNextLevel(); 24 }} 25 }
注意:記得將SMGameEnvironment.cs中的23行中的DemoConfig改為MyConfig
其中SMGameEnvironment.cs腳本如下:
1 using UnityEngine; 2 using System.Collections; 3 4 public class SMGameEnvironment { 5 private static SMGameEnvironment instance; 6 private SMSceneManager sceneManager; 7 public static SMGameEnvironment Instance { 8 get { 9 if (instance == null) { 10 instance = new SMGameEnvironment(); 11 } 12 return instance; 13 } 14 } 15 16 public SMSceneManager SceneManager { 17 get { 18 return sceneManager; 19 } 20 } 21 22 public SMGameEnvironment() { 23 sceneManager = new SMSceneManager(SMSceneConfigurationLoader.LoadActiveConfiguration("MyConfig")); 24 sceneManager.LevelProgress = new SMLevelProgress(sceneManager.ConfigurationName); 25 // Comment this in to enable the blinds transition type 26 // sceneManager.TransitionPrefab = "Transitions/MyTransition"; 27 } 28 }