如何为预制件创建枚举以使用精灵图像实例化
how to create an enum for a prefab to be instantiated with an image for sprite
基本上我创建了一个预制件,“badGuy”,共有三种类型。每个都有自己的精灵图像。所以我在附加到我的 badGuy 预制件的脚本中创建了以下枚举:
public enum BadGuyType {
green,
blue,
red
}
自从我没有枚举以来,我一直在为我的预制件使用一张图片。 badGuy 游戏对象有一个 public 属性 让我通过检查器添加它并实例化它多次,如下所示:
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);
}
如何在实例化我的 badGuy 游戏对象时为我的 BadGuyType 枚举和 select 设置图像(最好以编程方式)?
如果您希望每个 badGuy 都有自己的属性,那么您需要为 badguys 创建一个通用 class 并将所有变量放入其中。创建一个名为 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
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
}
基本上我创建了一个预制件,“badGuy”,共有三种类型。每个都有自己的精灵图像。所以我在附加到我的 badGuy 预制件的脚本中创建了以下枚举:
public enum BadGuyType {
green,
blue,
red
}
自从我没有枚举以来,我一直在为我的预制件使用一张图片。 badGuy 游戏对象有一个 public 属性 让我通过检查器添加它并实例化它多次,如下所示:
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);
}
如何在实例化我的 badGuy 游戏对象时为我的 BadGuyType 枚举和 select 设置图像(最好以编程方式)?
如果您希望每个 badGuy 都有自己的属性,那么您需要为 badguys 创建一个通用 class 并将所有变量放入其中。创建一个名为 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
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
}