扩展 Shader 编辑器

Shader "Unlit/Test"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_color("Color", Color) = (1,1, 1, 1)
		[HideInInspector] _QueueTag ("QueueTag", Float) = 0.0
		[HideInInspector] _QueueTagOffset ("QueueTagOffset", Float) = 0.0
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
		ZWrite Off
		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#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;
			float4 _color;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = mul(UNITY_MATRIX_MVP, 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);
				return _color;
			}
			ENDCG
		}

		
	}
	CustomEditor "CustomShaderGUI"
}


using UnityEngine;
using UnityEditor;
using System;

public class CustomShaderGUI : ShaderGUI
{
    public enum QueueTag
    {
        Background,
        Geometry,
        AlphaTest,
        Transparent,
        Overlay
    }

    public static readonly string[] queueTagNames = Enum.GetNames(typeof(QueueTag));

    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
    {
        base.OnGUI(materialEditor, properties);

        MaterialProperty queueTag = FindProperty("_QueueTag", properties);
        MaterialProperty queueTagOffset = FindProperty("_QueueTagOffset", properties);

        int qt = (int)queueTag.floatValue;
        int qtOffset = (int)queueTagOffset.floatValue;

        bool isModifyQueueTag = false;

        EditorGUI.BeginChangeCheck();
        qtOffset = EditorGUILayout.IntField("QueueTagOffset", qtOffset);
        if (EditorGUI.EndChangeCheck())
        {
            materialEditor.RegisterPropertyChangeUndo("QueueTagOffset");
            queueTagOffset.floatValue = (float)qtOffset;
            isModifyQueueTag = true;
        }


        EditorGUI.BeginChangeCheck();
        qt = EditorGUILayout.Popup("QueueTag", qt, queueTagNames);
        if (EditorGUI.EndChangeCheck())
        {
            materialEditor.RegisterPropertyChangeUndo("QueueTag");
            queueTag.floatValue = (float)qt;
            isModifyQueueTag = true;
            
        }

        if (isModifyQueueTag)
        {
            foreach (Material mat in queueTag.targets)
            {
                ModifyQueurTag(mat, (QueueTag)qt, qtOffset);
                EditorUtility.SetDirty(mat);
            }
        }

        
        
        
    }

    public void ModifyQueurTag(Material mat, QueueTag qt, int offset)
    {
        mat.renderQueue = offset;
        switch (qt)
        {
            case QueueTag.Background:
                mat.renderQueue += 1000;
                break;
            case QueueTag.Geometry:
                mat.renderQueue += 2000;
                break;
            case QueueTag.AlphaTest:
                mat.renderQueue += 2450;
                break;
            case QueueTag.Transparent:
                mat.renderQueue += 3000;
                break;
            case QueueTag.Overlay:
                mat.renderQueue += 4000;
                break;

        }
    }
}



你可能感兴趣的:(unity3d)