首页 > 代码库 > 遍历所有子物体中renderer(渲染器)中的material(材质)并改变其alpha值实现若隐若现的效果

遍历所有子物体中renderer(渲染器)中的material(材质)并改变其alpha值实现若隐若现的效果

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class CubeControl : MonoBehaviour
{

  public GameObject Cube;
  private float rotaSpeed=5f;
  void Update ()
  {

    //得到所有可渲染的子物体
    Renderer[] rds = Cube.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;
        var alpha = Mathf.PingPong(Time.time / 5f, 1);
        material.color = new Color(r, g, b, alpha);

      }
    }

    //Cube自转
    Cube.transform.Rotate(new Vector3(0, -1, 0), Time.deltaTime * rotaSpeed);
  }
}

遍历所有子物体中renderer(渲染器)中的material(材质)并改变其alpha值实现若隐若现的效果