unity 改变模型及其子物体的Shader和透明度

 /// 
        /// 改变透明度
        /// 
        /// 
        public virtual void OnYangFangZhiWu(GameObject  Obj, BaseEventData data = null)
        {
            Debug.Log("name=" + Obj.name);

            Renderer[] renderers = ZhiWu_Parent.transform.Find(Obj.name).GetComponentsInChildren();

            foreach (Renderer renderer in renderers)
            {
                Material[] materials = renderer.materials;

                foreach (Material material in materials)
                {
                    // 处理每个材质球
                    Debug.Log(material.name);

                    if (Obj.name == "牛鞭草")
                    {
                        renderer.material = material_TouMing;
                    }
                    else
                    {
                        // 获取新的Shader资源
                        Shader newShader = Shader.Find("Standard");
                        // 将新的Shader赋值给材质的shader属性
                        material.shader = newShader;
                     
                        MaterialModify.SetMaterialRenderingMode(material, MaterialModify.RenderingMode.Fade);
                        material.color = new Color(material.color.r, material.color.g, material.color.b, 0.2f);
                    }
                  
                }
            }

         
        }
///还原
 public void SetTree8_00()
        {
            for (int i = 0; i < ZhiWu_Parent.transform.childCount; i++)
            {
                int x = i;
                Renderer[] renderers = ZhiWu_Parent.transform.GetChild(x).GetComponentsInChildren();

                foreach (Renderer renderer in renderers)
                {
                    Material[] materials = renderer.materials;

                    foreach (Material material in materials)
                    {
                        // 处理每个材质球
                        Debug.Log(material.name);

                        if (ZhiWu_Parent.transform.GetChild(x).gameObject.name == "牛鞭草")
                        {
                            renderer.material = material_YuanBen;
                        }
                        else
                        {
                            // 获取新的Shader资源
                            //Shader newShader = Shader.Find("Nature/SpeedTree8");

                            // 将新的Shader赋值给材质的shader属性
                            material.shader = Shader.Find("Nature/SpeedTree8");
                            material.color = new Color(material.color.r, material.color.g, material.color.b, 1f);
                            // 将修改后的材质赋值给模型的渲染器组件
                            // renderer.material = material; 
                        }


                    }
                }
            }
        }

你可能感兴趣的:(unity)