简体   繁体   English

Unity3d - 尝试通过 C# 更改材质的颜色

[英]Unity3d - Trying to change color of Material via C#

I've looked all over for a solution for this and it seems simple enough but I don't understand why I can't change the property of one of the materials located on one of my NPC models.我已经到处寻找解决方案,它看起来很简单,但我不明白为什么我不能更改位于我的一个 NPC 模型上的一种材料的属性。

To put it simple - I am just trying to change the "_SkinColor" property of what I believe is the material shader?简单地说——我只是想改变我认为是材质着色器的“_SkinColor”属性? I guess because using the code shown will change the skin color to black?我猜是因为使用显示的代码会将肤色更改为黑色?

Thank you so much for your help!非常感谢你的帮助!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class colorchanger : MonoBehaviour
{
    [SerializeField] private Material myMaterial;

    //    [SerializeField]

    
    private float red = Random.Range(180, 231);
    private float green = Random.Range(200, 255);
    private float blue = Random.Range(50, 100);


    // Start is called before the first frame update
    void Start()
    {
        Color newColor = new Color(red,green,blue);
        myMaterial.SetColor("_SkinColor",newColor);
    }

    // Update is called once per frame
    void Update()
    {
    
    }

}

You will need to set the color of the material via its Renderer component.您需要通过其 Renderer 组件设置材质的颜色。

If the script is attached to the GameObject that has the renderer which is handling your material try:如果脚本附加到具有正在处理您的材质的渲染器的游戏对象,请尝试:

void Start()
{
    Renderer myRenderer = GetComponent<Renderer>();
    Color newColor = new Color(red,green,blue);
    myRenderer.material.color = newColor;
}

If the script is intended to change the colour of whatever material is passed in to it, you need to pass in the relevant Renderer rather than the material.如果脚本旨在更改传入的任何材质的颜色,则需要传入相关的渲染器而不是材质。 For example:例如:

public class MaterialColourChanger: MonoBehaviour {
    
    public void ChangeMaterialColour(Renderer renderer, Color colour) {
        renderer.material.color = colour;
    }
}

The call it from any other script:从任何其他脚本调用它:

public class AnyOtherScript: MonoBehaviour {
    
    MaterialColourChanger colourChanger;
    
    void Start() {
        colourChanger = GameObject.Find("TheColourChangerGameObject").GetComponent<MaterialColourChanger>();
    
        // Get the renderer component for the object that this script is attached to...
        Renderer thisObjectsRenderer = GetComponent<Renderer>();
        Color myColour = Color.red;
        
        // Pass it all to the colour changer script to change the colour
        colourChanger.ChangeMaterialColour(thisObjectsRenderer , myColour);
    }
}

If you are sure of the correct name of the property shader, it's because random numbers are float's between 0, 1 , not up to 255. fix it:如果您确定属性着色器的正确名称,那是因为随机数是介于0, 1之间的浮点数,而不是 255。修复它:

[SerializeField] private Material myMaterial;
void Start()
{
    var red = Random.Range(180, 231)/255f;
    var green = Random.Range(200, 255)/255f;
    var blue = Random.Range(50, 100)/255f;

    var newColor = new Color(red,green,blue);
    myMaterial.SetColor("_SkinColor",newColor);
}

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

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