遍历所有子物体中renderer(渲染器)中的material(材质) - U3DEngineer

时间:2024-04-15 15:40:18

遍历所有子物体中renderer(渲染器)中的material(材质)

//得到所有可渲染的子物体
Renderer[] rds = transform.GetComponentsInChildren<Renderer>();
//逐一遍历他的子物体中的Renderer
foreach (Renderer render in rds)
{
//逐一遍历子物体的子材质(renderer中的material
    foreach (Material material in render.materials)
{

      var r = material.color.r;
      var g = material.color.g;
      var b = material.color.b;
      material.color = new Color(r, g, b, _Alpha);

    }
}
附加的判断
var render = transform.GetComponent<Renderer>();

if (render != null)
{
  foreach (Material material in render.materials)
  {
    var r = material.color.r;
    var g = material.color.g;
    var b = material.color.b;
    material.color = new Color(r, g, b, _Alpha);
  }
}