如何创建一个使用sprite图像实例化的预制的枚举 [英] how to create an enum for a prefab to be instantiated with an image for sprite

查看:182
本文介绍了如何创建一个使用sprite图像实例化的预制的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我创建了一个预制,badGuy有三种类型。每个都有自己的精灵图片。所以我在附加到我的badGuy预制的脚本中创建了以下枚举:

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
}

我一直在使用因为我没有一个枚举,所以我的预制是一个图像。 badGuy游戏对象有一个公共属性,我可以通过检查器添加它,并实例化了很多次,如下所示:

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);
}

如何为我的BadGuyType枚举设置图像(最好是编程),然后选择它们实例化我的badGuy游戏对象?

How can I set images (preferably programmatically) for my BadGuyType enum and select them when instantiating my badGuy game objects?

推荐答案

如果您希望每个badGuy拥有自己的属性,那么您需要创建一个通用类对于badguys并把所有的变量放在里面。创建一个名为BadGuysManager的脚本,然后在其中执行所有坏人的操作,或从下面复制一个。

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.

功能签名:

void createBadGuy(GameObject badGuyPrefab, BadGuyType badGuyType, Vector3 badGuyPosition)
void setBadGuyType(BadGuyType badGuyType)
BadGuyType getBadGuyType()
void killBadGuy()
GameObject getBadGuyGameObject()

在你的 test.cs 脚本,您可以添加带有列表的BadGuysManager脚本。

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();

    }
}

下面的代码应该在你的 BadGuysManager .cs脚本。这应该让你开始。您可以轻松地扩展或添加更多功能到 BadGuysManager 类。

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
}

这篇关于如何创建一个使用sprite图像实例化的预制的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆