如图所示,c#脚本控制shader颜色。
public class ControlColor : MonoBehaviour
{
public Color color = new Color(, , , );
public Material mat;
public void ChangeColor()
{
mat.SetVector("_Diffuse", color);
}
}
[CustomEditor(typeof(ControlColor))]
public class Edit_ControlColor : Editor {
ControlColor scr;//脚本本体
SerializedObject serObj;
SerializedProperty color;
SerializedProperty mat; public void OnEnable()
{
scr = (ControlColor)target;
serObj = new SerializedObject(target);
color = serObj.FindProperty("color");
mat = serObj.FindProperty("mat");
}
public override void OnInspectorGUI()
{
serObj.Update();
EditorGUILayout.PropertyField(color,new GUIContent("Color"));
EditorGUILayout.PropertyField(mat,new GUIContent("Mat"));
serObj.ApplyModifiedProperties();
scr.ChangeColor();
}
}
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' Shader "Custom/Color"
{
Properties
{
_Diffuse("Diffuse",Color)=(,,,)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc"
float4 _Diffuse; struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
}; struct v2f
{
float4 pos : SV_POSITION;
}; v2f vert (appdata v)
{
v2f o;
o.pos=UnityObjectToClipPos(v.vertex);
return o;
} fixed4 frag (v2f i) : SV_Target
{
return _Diffuse;
}
ENDCG
}
}
}