赞
踩
目录
1.1、利用ShaderGUI派生类来重新绘制Shader的Inspector窗口
1.2、利用MaterialPropertyDrawers,即加标签形式扩展
- Shader "Unlit/BaseUnityShader"
- {
- Properties
- {
- _MainTex("Texture", 2D) = "white" {}
- //[Toggle(REDIFY_ON)] _Redify("Red?", Int) = 0
- }
- SubShader
- {
- Tags { "RenderType" = "Opaque" }
- LOD 200
-
- CGPROGRAM
- #pragma surface surf Lambert addshadow
- #pragma shader_feature REDIFY_ON
- sampler2D _MainTex;
- struct Input {
- float2 uv_MainTex;
- };
- void surf(Input IN, inout SurfaceOutput o)
- {
- half4 c = tex2D(_MainTex, IN.uv_MainTex);
- o.Albedo = c.rgb;
- o.Alpha = c.a;
-
- #if REDIFY_ON
- o.Albedo.gb *= 0.5;
- #endif
- }
- ENDCG
- }
- CustomEditor "UnityShaderEditorUI" //指向继承于ShaderGUI的类,进行编辑Shader的Inspector窗口
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System;
-
- public class UnityShaderEditorUI : ShaderGUI
- {
- static string KEY = "REDIFY_ON";
- public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
- {
- base.OnGUI(materialEditor, properties);
-
- Material targetMat = materialEditor.target as Material;
-
- bool redify = Array.IndexOf(targetMat.shaderKeywords, KEY) != -1;
- EditorGUI.BeginChangeCheck();
- redify = EditorGUILayout.Toggle("Redify material", redify);
- if (EditorGUI.EndChangeCheck())
- {
- if (redify)
- {
- targetMat.EnableKeyword(KEY);
- }
- else
- {
- targetMat.DisableKeyword(KEY);
- }
- }
- }
- }
- Shader "Unlit/BaseUnityShader"
- {
- Properties
- {
- _MainTex("Texture", 2D) = "white" {}
- //Toggle标签,REDIFY_ON是Shader变体,设置该属性会直接赋值到变体上(控制开关)
- [Toggle(REDIFY_ON)] _Redify("Red?", Int) = 0
- }
- SubShader
- {
- Tags { "RenderType" = "Opaque" }
- LOD 200
-
- CGPROGRAM
- #pragma surface surf Lambert addshadow
- //Shader变体 REDIFY_ON
- #pragma shader_feature REDIFY_ON
- sampler2D _MainTex;
- struct Input {
- float2 uv_MainTex;
- };
- void surf(Input IN, inout SurfaceOutput o)
- {
- half4 c = tex2D(_MainTex, IN.uv_MainTex);
- o.Albedo = c.rgb;
- o.Alpha = c.a;
-
- #if REDIFY_ON
- o.Albedo.gb *= 0.5;
- #endif
- }
- ENDCG
- }
- //CustomEditor "UnityShaderEditorUI" //指向继承于ShaderGUI的类,进行编辑Shader的Inspector窗口
- }
- Shader "Unlit/DrawerUnityShader"
- {
- Properties
- {
- _MainTex ("Texture", 2D) = "white" {}
- //Toggle
- [Toggle] _Toggle("Tog?", Float) = 0
- [Toggle(ENABLE_FANCY)] _Fancy("Fancy", Float) = 0
-
- [Enum(UnityEngine.ShaderMilk.ShaderEnum)] _ShaderEnum("ShaderEnum", Float) = 1
- [Enum(Apple, 1, Banana, 2)] _Flood("Flood", Float) = 1
-
- //三个变种key名称为:属性名+_+枚举名(全大写) 如:_OVERLAY_NONE
- [KeywordEnum(None, Add, Multiply)] _Overlay("Overlay", Float) = 0//疑问:如何不选择任意一个变体 为空
-
- //非线性滑动条
- [PowerSlider(3.0)] _Shininess("Shininess", Range(0.01, 1)) = 0.08
-
- [IntRange] _Alpha ("Alpha", Range(0, 255)) = 100
-
- [Space] _Prop1 ("Prop1", Float) = 0
- [Space(50)] _Prop2 ("Prop2", Float) = 0
-
- [Header(Test)] _Header1("Header1" , Float) = 0
- }
- SubShader
- {
- Tags { "RenderType"="Opaque" }
- LOD 100
-
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- // make fog work
- #pragma multi_compile_fog
- #pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY
- #pragma multi_compile _ ENABLE_FANCY
- //#pragma shader_feature ENABLE_FANCY
-
- #include "UnityCG.cginc"
-
- struct appdata
- {
- float4 vertex : POSITION;
- float2 uv : TEXCOORD0;
- };
-
- struct v2f
- {
- float2 uv : TEXCOORD0;
- UNITY_FOG_COORDS(1)
- float4 vertex : SV_POSITION;
- };
-
- sampler2D _MainTex;
- float4 _MainTex_ST;
- float _ShaderEnum;
-
- v2f vert (appdata v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- o.uv = TRANSFORM_TEX(v.uv, _MainTex);
- UNITY_TRANSFER_FOG(o,o.vertex);
- return o;
- }
-
- fixed4 frag (v2f i) : SV_Target
- {
- // sample the texture
- fixed4 col = tex2D(_MainTex, i.uv);
- // apply fog
- UNITY_APPLY_FOG(i.fogCoord, col);
- if (_ShaderEnum == 1) {
- col = fixed4(1, 0, 0, 1);
- }else if (_ShaderEnum == 4) {
- col = fixed4(0, 1, 0, 1);
- }
- #if _OVERLAY_NONE
- col = fixed4(1, 1, 0, 1);
- #elif _OVERLAY_ADD
- col = fixed4(1, 0, 1, 1);
- #elif _OVERLAY_MULTIPLY
- col = fixed4(1, 0.5, 1, 1);
- #else
- col = fixed4(0, 0, 0, 1);
- #endif
-
- return col;
- }
- ENDCG
- }
- }
- }
更多知识可参考Unity官方手册Custom Shader GUI、MaterialPropertyDrawers等
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。