【问题标题】:How to test what material is applied to an object in Unity如何在 Unity 中测试将什么材质应用于对象
【发布时间】:2016-08-03 15:23:37
【问题描述】:

我有一个钻石精灵,我希望能够将钻石的颜色从白色更改为绿色。但是,我无法弄清楚如何做到这一点。

    public class MoveControl : MonoBehaviour {
    // Update is called once per frame
    void Update () {
    if (Input.GetKey(KeyCode.A) )
    {
        if (GetComponent<Renderer>().material.color == Color.white)
        {
            GetComponent<Renderer>().material.color = Color.green;
        }  
    }

  }

}

上面的代码是我现在所拥有的,它只有在应用于精灵的材质(白色)是精灵/默认着色器时才有效。这听起来可能不是什么大问题,但是每当我应用不同颜色的不同材质(例如蓝色)并更改其设置以使其具有精灵/默认着色器时,精灵就会变得不可见。

我是 Unity 的新手,如果有人可以帮助我,将不胜感激

【问题讨论】:

    标签: if-statement unity3d


    【解决方案1】:

    我不确定您要做什么,但这可能会对您有所帮助。

    public class Material : MonoBehaviour {
    
    Renderer renderer;
    Color currentColor;
    Color originalColor;
    
    // Use this for initialization
    void Start () {
    
        renderer = gameObject.GetComponent<Renderer>(); //If you don't know the gameObject is the object this script is attached to
        originalColor = renderer.material.color; //The original color ( in your case white )
    
    }
    
    // Update is called once per frame
    void Update () {
    
    
        if (Input.GetKey(KeyCode.A)) {
    
            if (renderer.material.color == originalColor) { //Here we are checking if the color on the object is the original color
                renderer.material.color = Color.blue;   
                currentColor = renderer.material.color; //Here we are assining the current color
            }
    
        }
    
    }
    

    }

    我创建了一个材质并将其分配给编辑器中的游戏对象。

    【讨论】:

    猜你喜欢
    • 2016-02-23
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 2018-09-21
    • 1970-01-01
    • 2013-12-08
    相关资源
    最近更新 更多