简体   繁体   English

更改实例化预制 Unity 的颜色

[英]Change the color of an instantiated prefab Unity

so the player hits a button to create a box.所以玩家点击一个按钮来创建一个盒子。 I need this box to be randomly varying between a few colors.我需要这个盒子在几个 colors 之间随机变化。 I also need this box to have a tag corresponding to said color.我还需要这个盒子有一个与所述颜色相对应的标签。 Green box - "greenBlock" tag etc.绿框 - “greenBlock”标签等。

I have instantiated the box and then try to change it's material with material.color.我已经实例化了这个盒子,然后尝试用 material.color 改变它的材质。 It doesn't do anything.它什么也没做。 I've seen suggestions of sharedMaterial but having tried that found it just ends up changing the color of every game object in the scene.我已经看到了 sharedMaterial 的建议,但尝试过发现它最终会改变场景中每个游戏 object 的颜色。 I think I'm fetching the box prefabs renderer correctly?我想我正确地获取了盒子预制渲染器? Any help would be appreciated!任何帮助,将不胜感激!

Here's what I have so far:这是我到目前为止所拥有的:


public class ButtonScript : MonoBehaviour
{
    public GameObject Box;
    public Transform spawnpoint1;
    public Transform spawnpoint2;
    public Rigidbody2D player;
    public Renderer boxRenderer;

    [SerializeField]
    private Color boxColor;
    [SerializeField]
    private AudioSource actionSound;

    // Update is called once per frame
    private void Start()
    {
        //boxRenderer = Box.gameObject.GetComponent<Renderer>();
        boxRenderer = GameObject.Find("Box").GetComponent<Renderer>();  //  Find the renderer of the box prefab
    }


    public void OnTriggerStay2D(Collider2D col)
    {
        if (player) //  If it's the player in the collider trigger
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                Instantiate(Box, spawnpoint1.position, Quaternion.identity);
                boxRenderer.material.color = boxColor;  //  change the color after it is instantiated
                actionSound.Play();
            }
        }

    }

}

boxRenderer.material.SetColor("_Color", boxColor);

or或者

boxRenderer.material.color = new Color(0.5, 0.5, 0.0, 1.0);

And when you instantiate the box, you need to get the render for the box at this point as it is a new object.当你实例化盒子时,此时你需要得到盒子的渲染,因为它是一个新的 object。 So:所以:

       if (Input.GetKeyDown(KeyCode.E))
        {
            Box = Instantiate(Box, spawnpoint1.position, Quaternion.identity);
            boxRenderer = Box.transform.GetComponent<Renderer>();
            boxRenderer.material.color = boxColor;  //  change the color after it is instantiated
            actionSound.Play();
        }

Note you have creating a New color and assigning it, you can't modify the color there as it may be used on other objects that are using the same material.请注意,您已经创建了新颜色并分配了它,您不能在那里修改颜色,因为它可能用于使用相同材料的其他对象。

Check out SetColor in the docs, that is setting the shader property called _Color which is the default color element in shaders, you can of course have more depending on the shader.查看文档中的SetColor ,即设置名为_Color的着色器属性,这是着色器中的默认颜色元素,您当然可以根据着色器拥有更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM