问题
Basically I’ve created a prefab, “badGuy” of which there are three types. Each has it’s their own sprite images. So I created the following enum in a script attached to my badGuy prefab:
public enum BadGuyType {
green,
blue,
red
}
All along I have been using one image for my prefab since I didn’t have a enum. The badGuy game object had a public property for me to add it through the inspector and instantiate it a number of times as below:
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);
}
How can I set images (preferably programmatically) for my BadGuyType enum and select them when instantiating my badGuy game objects?
回答1:
If you want each badGuy to have its own properties then you need to create a general class for badguys and put all the variables inside it. Create a Script called BadGuysManager then implement all your bad guy's actions inside there or copy the one from below.
Function signatures:
void createBadGuy(GameObject badGuyPrefab, BadGuyType badGuyType, Vector3 badGuyPosition)
void setBadGuyType(BadGuyType badGuyType)
BadGuyType getBadGuyType()
void killBadGuy()
GameObject getBadGuyGameObject()
In your test.cs
script, you can add the BadGuysManager script with a list.
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();
}
}
The code below should be in your BadGuysManager
.cs script. This should get you started. You can easily extend or add more functions to the BadGuysManager
class.
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
}
来源:https://stackoverflow.com/questions/36702497/how-to-create-an-enum-for-a-prefab-to-be-instantiated-with-an-image-for-sprite