如图所示,c#脚本控制shader颜色。
1 public class ControlColor : MonoBehaviour2 {3 public Color color = new Color(1, 1, 1, 1);4 public Material mat;5 public void ChangeColor()6 {7 mat.SetVector("_Diffuse", color);8 }9 }
1 [CustomEditor(typeof(ControlColor))] 2 public class Edit_ControlColor : Editor { 3 ControlColor scr;//脚本本体 4 SerializedObject serObj; 5 SerializedProperty color; 6 SerializedProperty mat; 7 8 public void OnEnable() 9 {10 scr = (ControlColor)target;11 serObj = new SerializedObject(target);12 color = serObj.FindProperty("color");13 mat = serObj.FindProperty("mat");14 }15 public override void OnInspectorGUI()16 {17 serObj.Update();18 EditorGUILayout.PropertyField(color,new GUIContent("Color"));19 EditorGUILayout.PropertyField(mat,new GUIContent("Mat"));20 serObj.ApplyModifiedProperties();21 scr.ChangeColor();22 }23 }
1 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 2 3 Shader "Custom/Color" 4 { 5 Properties 6 { 7 _Diffuse("Diffuse",Color)=(1,1,1,1) 8 } 9 SubShader10 {11 Tags { "RenderType"="Opaque" }12 LOD 10013 14 Pass15 {16 CGPROGRAM17 #pragma vertex vert18 #pragma fragment frag19 20 #include "UnityCG.cginc"21 float4 _Diffuse;22 23 struct appdata24 {25 float4 vertex : POSITION;26 float2 uv : TEXCOORD0;27 };28 29 struct v2f30 {31 float4 pos : SV_POSITION;32 };33 34 v2f vert (appdata v)35 {36 v2f o;37 o.pos=UnityObjectToClipPos(v.vertex);38 return o;39 }40 41 fixed4 frag (v2f i) : SV_Target42 {43 return _Diffuse;44 }45 ENDCG46 }47 }48 }