Unity實戰 RTS3D即時戰略遊戲開發(二)
大家好,我是Zander,我們接著來開發Rts3D即時戰略性遊戲,本次所需要的資源大家可以在上篇文章所給的連結中下載。
首先、我們要在下載好的工程包中找到Scenes資料夾並開啟RTSMap場景。
然後 、我們來建立玩家和敵人的出生點位置,如圖所示,在Map中建立兩個空物體,分別取名為Player1Start,Player2Start,為了方便我們看到已定義好的Player1Start和Player2Start,我們從檢視面板的左上角選擇ICON,分別選擇一個顏色Icon
場景弄好了,現在我們來管理我們的場景,首先在Assets下建立一個資料夾Scripts,然後再Scripts下再建立一個叫RtsManager的指令碼,它儲存並管理著整個遊戲中的大部分有用的功能,其他部分需要訪問或者查詢,我們把它設計成一個單例模式
using UnityEngine;
using System.Collections;
public class RtsManager : MonoBehaviour {
public static RtsManager Current = null;
// Use this for initialization
void Start () {
Current = this;
}
// Update is called once per frame
void Update () {
}
}
然後返回Unity中,在Hierarchy中建立一個空物件並命名Manager,之所以叫Manager是因為上面要掛載一些管理指令碼。RTSManager就是其中之一,現在就來新增指令碼,把寫好的RTSManager指令碼直接拖拽到Hierarchy中的Manager上即可。如圖
下面我們來建立一個Player類來持有一些玩家所需的資料,在Scripts新建一個Definitions資料夾,在其底下新建一個PlayerSetupDefinition指令碼,這個指令碼不需要繼承於MonoBehavior,但是為了讓這個類易讀並能在檢視面板上可見,所以我們需要把它序列化。在這個指令碼中,我們需要定義玩家的姓名,起始的位置、要新增一些顏色或標識等來區分它們、然後還需要一個初始單位列表,接下來還要知道它需不需要Ai控制、玩家積分,然後儲存。
using UnityEngine; using System.Collections.Generic; [System.Serializable] public class PlayerSetupDefinition { public string Name; //玩家名字 public Transform Location; //起始位置 public Color AccentColor; //玩家標識顏色 public List<GameObject> StartingUnits = new List<GameObject>(); public bool IsAi; //是不是AI控制 public float Credits; //積分 }
然後切換到RTSManager中,來定義這個指令碼進行管理
using UnityEngine;
using System.Collections.Generic;
public class RtsManager : MonoBehaviour {
public static RtsManager Current = null;
public List<PlayerSetupDefinition> Players = new List<PlayerSetupDefinition>();
// Use this for initialization
void Start () {
Current = this;
}
// Update is called once per frame
void Update () {
}
}
定義完後,來到Unity中,能看到Players的初始值為0,我們把它改為2,然後把Player1和Player 2的屬性設定一下如圖:
這節我們先說到這來,下面是本次的連結:連結: https://pan.baidu.com/s/1gfqPc6R 密碼: 9xkm
歡迎大家加入QQ群:280993838 或者關注我的公眾微訊號