如图所示,c#脚本控制shader颜色。
1 public class ControlColor : MonoBehaviour 2 { 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 SubShader 10 { 11 Tags { "RenderType"="Opaque" } 12 LOD 100 13 14 Pass 15 { 16 CGPROGRAM 17 #pragma vertex vert 18 #pragma fragment frag 19 20 #include "UnityCG.cginc" 21 float4 _Diffuse; 22 23 struct appdata 24 { 25 float4 vertex : POSITION; 26 float2 uv : TEXCOORD0; 27 }; 28 29 struct v2f 30 { 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_Target 42 { 43 return _Diffuse; 44 } 45 ENDCG 46 } 47 } 48 }