Skip to content

Instantly share code, notes, and snippets.

@gatosyocora
Last active April 20, 2023 08:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gatosyocora/9f506612a27a02e28e0be60d09535580 to your computer and use it in GitHub Desktop.
Save gatosyocora/9f506612a27a02e28e0be60d09535580 to your computer and use it in GitHub Desktop.
ShaderLabのCustomMaterialPropertyDrawerのサンプル(区切り線, Texture2Dの1行表示, foldout)
using UnityEngine;
using UnityEditor;
using System;
namespace GatoMaterialPropertyDrawer
{
internal class LineDecorator : MaterialPropertyDrawer
{
private float spaceSize;
public LineDecorator()
{
spaceSize = 0;
}
public LineDecorator(float spaceSize)
{
this.spaceSize = spaceSize;
}
public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
GUILayout.Space(spaceSize);
GUILayout.Box(string.Empty, GUILayout.ExpandWidth(true), GUILayout.Height(1));
GUILayout.Space(spaceSize);
}
}
internal class SingleLineTexDrawer : MaterialPropertyDrawer
{
public SingleLineTexDrawer() { }
public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
editor.TexturePropertySingleLine(label, prop);
}
}
internal class FoldoutDecorator : MaterialPropertyDrawer
{
public bool isFoldout = false;
private bool useIndent = true;
private string groupKeyword;
public FoldoutDecorator(string groupKeyword)
{
this.groupKeyword = groupKeyword;
}
public FoldoutDecorator(string groupKeyword, float isFoldout)
{
this.groupKeyword = groupKeyword;
this.isFoldout = Convert.ToBoolean(isFoldout);
}
public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
var style = new GUIStyle("ShurikenModuleTitle");
style.font = new GUIStyle(EditorStyles.label).font;
style.border = new RectOffset(15, 7, 4, 4);
style.fixedHeight = 22;
style.contentOffset = new Vector2(20f, -2f);
var rect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
GUI.Box(rect, groupKeyword, style);
var e = Event.current;
var toggleRect = new Rect(rect.x + 4f, rect.y + 2f, 13f, 13f);
if (e.type == EventType.Repaint)
{
EditorStyles.foldout.Draw(toggleRect, false, false, isFoldout, false);
}
if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition))
{
isFoldout = !isFoldout;
e.Use();
}
var material = editor.target as Material;
material.SetFloat("_IsFoldout"+groupKeyword, Convert.ToSingle(isFoldout));
}
}
internal class FoldoutGroupDrawer : MaterialPropertyDrawer
{
private string groupKeyword;
public FoldoutGroupDrawer(string groupKeyword)
{
this.groupKeyword = groupKeyword;
}
public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor)
{
var material = editor.target as Material;
if (!material.HasProperty("_IsFoldout" + groupKeyword)) return 0f;
var isFoldout = Convert.ToBoolean(material.GetFloat("_IsFoldout" + groupKeyword));
if (!isFoldout)
{
return 0f;
}
else
{
return base.GetPropertyHeight(prop, label, editor);
}
}
public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
var material = editor.target as Material;
if (!material.HasProperty("_IsFoldout" + groupKeyword)) return;
var isFoldout = Convert.ToBoolean(material.GetFloat("_IsFoldout" + groupKeyword));
if (!isFoldout)
{
return;
}
EditorGUI.indentLevel++;
editor.DefaultShaderProperty(prop, label.text);
EditorGUI.indentLevel--;
}
}
}
Shader "Unlit/PropertyTest"
{
Properties
{
[SingleLineTex]
_TestTex ("TestTex", 2D) = "while" {}
[Line(10)]
_Color ("Color", Color) = (1, 1, 1, 1)
[Foldout(Test)]
[FoldoutGroup(Test)]
_Color2("Color2", Color) = (1, 1, 1, 1)
[FoldoutGroup(Test)]
_MainTex ("Texture", 2D) = "white" {}
[FoldoutGroup(Test)]
_FloatValue("Float", Range(0, 1)) = 1
[Foldout(Test2)]
[FoldoutGroup(Test2)]
_Color3("Color3", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
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;
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);
return col;
}
ENDCG
}
}
}

実装機能

  • 区切り線 [Line] [Line(上下余白)]
  • Texture2Dの1行表示 [SingleLineTex]
  • 折りたたむ
    • 最初の要素に [Foldout(グループ名)]
    • 各要素に [FoldoutGroup(グループ名)]

導入方法

  1. Editorフォルダを作成してGatoMaterialPropertyDrawer.csを入れる
  2. Shaderコードで[Line]や[SingleLineTex]などを書く(PropertyTest.shaderを参照)

unitypackageの配布

https://drive.google.com/open?id=1EK-FVzLDklNo9kuEqW2mUW9r4l_dVS-U

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment