Unity3d 如何找到游戏对象并改变其颜色

 

 

//游戏对象

private var obj:GameObject;

//渲染器

private var render:Renderer;

//贴图

private var texture:Texture;



function Start () 

{

    //获取游戏对象

    obj = GameObject.Find("Cube");

    //获取该游戏对象的渲染器

    render = obj.GetComponent("Renderer");

}



 function OnGUI()

 {

     if (GUILayout.Button("Color",GUILayout.Width(100),GUILayout.Height(50)))

     {

         //改变渲染的颜色

         render.material.color = Color.green;

         //添加组件贴图

         render.material.mainTexture = null;

     }

     

     

     if(GUILayout.Button("add",GUILayout.Width(100),GUILayout.Height(50)))

     {

         

         render.material = null;

         render.material.mainTexture = texture;

     }

 }  

 更改Shader

,如NewShader

Shader "Custom/NewShader" {

    Properties {

        _Color ("Main Color", COLOR) = (1,1,1,1)

        _AmbientColor("Ambient Color",COLOR) =(0,1,0,1)

    }

    SubShader {

        Pass {

            Material {

                Diffuse [_Color]

                Ambient [_AmbientColor]

            }

            Lighting On

        }

    }

}

在代码中设置颜色

        CubeObj.renderer.material.shader = Shader.Find("Custom/NewShader");     //设置材质的shader

        print(CubeObj.renderer.material.GetColor("_AmbientColor"));  //获取_AmbientColor 的颜色

        CubeObj.renderer.material.SetColor("_AmbientColor", Color.red);  //设置_AmbientColor的颜色

 Unity3d 如何找到游戏对象并改变其颜色

 

 

                //Renderer[] renders = obj.GetComponentsInChildren<Renderer>();



                //foreach (Renderer one in renders)

                //{

                //    one.material.shader = Shader.Find("Custom/NewShader");

                //    Color changeColor = new Color(1,0.33f,1,1);

                //    one.material.SetColor("_IlluminCol", changeColor);  //设置颜色

                //}

 

你可能感兴趣的:(unity3d)