基本上我已经创建了一个预制的"badGuy",它有三种类型。每个精灵都有自己的精灵图像。因此,我在我的坏蛋序言附带的脚本中创建了以下枚举:
public enum BadGuyType {
green,
blue,
red
}
一直以来,我都在使用一个图像作为我的预制,因为我没有枚举。badGuy游戏对象有一个公共属性,我可以通过检查器添加它,并将其实例化多次,如下所示:
public GameObject badGuy; //I set this in the inspector
void Start () {
badGuys= new List<GameObject> ();
int numberOfBadGuys = 6;
Camera camera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
for (int i = 1; i < numberOfBadGuys + 1; i++) {
GameObject badGuyObject = (GameObject)Instantiate(badGuy, new Vector3(Screen.width*i/2, Screen.height*i/6, camera.nearClipPlane ), Quaternion.identity );
badGuys.Add(badGuyObject);
}
如何为BadGuyType枚举设置图像(最好是以编程方式),并在实例化我的badGuy游戏对象时选择它们?
如果你想让每个坏蛋都有自己的属性,那么你需要为坏蛋创建一个通用类,并将所有变量放在里面。创建一个名为BadGuysManager的脚本,然后在里面实现坏蛋的所有操作,或者从下面复制一个。
功能签名:
void createBadGuy(GameObject badGuyPrefab, BadGuyType badGuyType, Vector3 badGuyPosition)
void setBadGuyType(BadGuyType badGuyType)
BadGuyType getBadGuyType()
void killBadGuy()
GameObject getBadGuyGameObject()
在test.cs
脚本中,您可以添加BadGuysManager脚本和列表。
public class test : MonoBehaviour
{
//Attch the gameObject bad guy here
public GameObject badGuyPrefab;
List<BadGuysManager> badGuys = new List<BadGuysManager>();
// Use this for initialization
void Start()
{
/////////////////////////////////////CREATE 3 bad guys with different colors
//Bad Guy 1, Green Color
badGuys.Add(gameObject.AddComponent<BadGuysManager>());
badGuys[0].createBadGuy(badGuyPrefab, BadGuyType.green, Vector3.zero);
//Bad Guy 2, Blue Color
badGuys.Add(gameObject.AddComponent<BadGuysManager>());
badGuys[1].createBadGuy(badGuyPrefab, BadGuyType.blue, Vector3.zero);
//Bad Guy 3, Red Color
badGuys.Add(gameObject.AddComponent<BadGuysManager>());
badGuys[2].createBadGuy(badGuyPrefab, BadGuyType.red, Vector3.zero);
/////////////////////////////////////CHANGE bad guy type later on
//badGuys[0].setBadGuyType(BadGuyType.green);
/////////////////////////////////////READ bad guy type
//BadGuyType badGuyType = badGuys[0].getBadGuyType();
/////////////////////////////////////Get bad guy
//GameObject badGuy = badGuys[0].getBadGuyGameObject();
/////////////////////////////////////KILL bad guy type
//badGuys[0].killBadGuy();
}
}
下面的代码应该在BadGuysManager
.cs脚本中。这应该会让你开始。您可以很容易地将更多函数扩展或添加到BadGuysManager
类中。
public class BadGuysManager : MonoBehaviour
{
private GameObject badGuy; //I set this in the inspector
private BadGuyType playerGuyType = BadGuyType.NONE;
public void setBadGuyType(BadGuyType badGuyType)
{
playerGuyType = badGuyType;
if (badGuy == null)
{
Debug.Log("Failed to set color because Bad Guy Prefab is null! Call createBadGuy function first");
return; //exit
}
if (badGuyType == BadGuyType.green)
{
badGuy.GetComponent<MeshRenderer>().material.color = Color.green;
}
else if (badGuyType == BadGuyType.blue)
{
badGuy.GetComponent<MeshRenderer>().material.color = Color.blue;
}
if (badGuyType == BadGuyType.red)
{
badGuy.GetComponent<MeshRenderer>().material.color = Color.red;
}
}
public BadGuyType getBadGuyType()
{
return playerGuyType;
}
public void createBadGuy(GameObject badGuyPrefab, BadGuyType badGuyType, Vector3 badGuyPosition)
{
if (badGuyPrefab == null)
{
Debug.Log("Bad Guy Prefab is null!");
return; //exit
}
badGuy = (GameObject)Instantiate(badGuyPrefab, badGuyPosition, Quaternion.identity);
setBadGuyType(badGuyType);
}
public void killBadGuy()
{
Destroy(badGuy);
Destroy(this);
}
public GameObject getBadGuyGameObject()
{
return badGuy;
}
}
public enum BadGuyType
{
NONE,
green,
blue,
red
}