Added GetEnumValue;
Added EnumFlagsDrawer; Added custom keywordName option to KeywordTexturePropertySingleLine;
This commit is contained in:
76
Editor/PropertyDrawer/EnumFlagsDrawer.cs
Normal file
76
Editor/PropertyDrawer/EnumFlagsDrawer.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace Misaki.ShaderGUI
|
||||
{
|
||||
public class EnumFlagsUIDrawer : MaterialPropertyDrawer
|
||||
{
|
||||
private readonly Type enumType;
|
||||
|
||||
public EnumFlagsUIDrawer(string enumTypeName, string assemblyName)
|
||||
{
|
||||
enumType = Type.GetType(Assembly.CreateQualifiedName(assemblyName, enumTypeName), true);
|
||||
}
|
||||
|
||||
private static bool IsPropertyTypeSuitable(MaterialProperty prop)
|
||||
{
|
||||
#if UNITY_6000_1_OR_NEWER
|
||||
return prop.propertyType == ShaderPropertyType.Float || prop.propertyType == ShaderPropertyType.Range || prop.propertyType == ShaderPropertyType.Int;
|
||||
#else
|
||||
return prop.type == MaterialProperty.PropType.Float || prop.type == MaterialProperty.PropType.Range || prop.type == MaterialProperty.PropType.Float;
|
||||
#endif
|
||||
}
|
||||
|
||||
public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
|
||||
{
|
||||
if (!IsPropertyTypeSuitable(prop))
|
||||
{
|
||||
var c = EditorGUIUtility.TrTempContent("Toggle used on a non-float property: " + prop.name);
|
||||
EditorGUI.LabelField(position, c, EditorStyles.helpBox);
|
||||
return;
|
||||
}
|
||||
|
||||
MaterialEditor.BeginProperty(position, prop);
|
||||
|
||||
#if UNITY_6000_1_OR_NEWER
|
||||
if (prop.propertyType != ShaderPropertyType.Int)
|
||||
#else
|
||||
if (prop.type != MaterialProperty.PropType.Int)
|
||||
#endif
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
var value = (int)prop.floatValue;
|
||||
EditorGUI.showMixedValue = prop.hasMixedValue;
|
||||
var enumValue = EditorGUI.EnumFlagsField(position, label, (Enum)Enum.ToObject(enumType, value));
|
||||
value = Convert.ToInt32(enumValue);
|
||||
EditorGUI.showMixedValue = false;
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
prop.floatValue = value;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
var value = prop.intValue;
|
||||
EditorGUI.showMixedValue = prop.hasMixedValue;
|
||||
var enumValue = EditorGUI.EnumFlagsField(position, label, (Enum)Enum.ToObject(enumType, value));
|
||||
value = Convert.ToInt32(enumValue);
|
||||
|
||||
EditorGUI.showMixedValue = false;
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
prop.intValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
MaterialEditor.EndProperty();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Editor/PropertyDrawer/EnumFlagsDrawer.cs.meta
Normal file
2
Editor/PropertyDrawer/EnumFlagsDrawer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 44cf7877640af1d4795a4d087824f9f5
|
||||
@@ -9,11 +9,11 @@ namespace Misaki.ShaderGUI
|
||||
{
|
||||
private static readonly GUIContent[] _options = { new("Disabled"), new("Enabled") };
|
||||
|
||||
protected virtual void SetKeyword(MaterialProperty prop, bool on)
|
||||
protected virtual void OnPropertyChange(MaterialProperty prop)
|
||||
{
|
||||
}
|
||||
|
||||
static bool IsPropertyTypeSuitable(MaterialProperty prop)
|
||||
private static bool IsPropertyTypeSuitable(MaterialProperty prop)
|
||||
{
|
||||
#if UNITY_6000_1_OR_NEWER
|
||||
return prop.propertyType == ShaderPropertyType.Float || prop.propertyType == ShaderPropertyType.Range || prop.propertyType == ShaderPropertyType.Int;
|
||||
@@ -41,14 +41,14 @@ namespace Misaki.ShaderGUI
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
var value = (int)Math.Abs(prop.floatValue);
|
||||
var value = (int)Mathf.Abs(prop.floatValue);
|
||||
EditorGUI.showMixedValue = prop.hasMixedValue;
|
||||
value = EditorGUI.Popup(position, label, value, _options);
|
||||
EditorGUI.showMixedValue = false;
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
prop.floatValue = value;
|
||||
SetKeyword(prop, value != 0);
|
||||
OnPropertyChange(prop);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -62,7 +62,7 @@ namespace Misaki.ShaderGUI
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
prop.intValue = value;
|
||||
SetKeyword(prop, value != 0);
|
||||
OnPropertyChange(prop);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ namespace Misaki.ShaderGUI
|
||||
{
|
||||
private const string Keyword_Suffix = "_ON";
|
||||
|
||||
protected override void SetKeyword(MaterialProperty prop, bool on)
|
||||
protected override void OnPropertyChange(MaterialProperty prop)
|
||||
{
|
||||
var keywordName = prop.name.ToUpper() + Keyword_Suffix;
|
||||
foreach (var target in prop.targets)
|
||||
@@ -84,7 +84,52 @@ namespace Misaki.ShaderGUI
|
||||
continue;
|
||||
}
|
||||
|
||||
material.SetKeyword(new LocalKeyword(material.shader, keywordName), on);
|
||||
#if UNITY_6000_1_OR_NEWER
|
||||
if (prop.propertyType != ShaderPropertyType.Int)
|
||||
#else
|
||||
if (prop.type != MaterialProperty.PropType.Int)
|
||||
#endif
|
||||
{
|
||||
material.SetKeyword(new LocalKeyword(material.shader, keywordName), prop.floatValue != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
material.SetKeyword(new LocalKeyword(material.shader, keywordName), prop.intValue != 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class PassPopupDrawer : PopupUIDrawer
|
||||
{
|
||||
private readonly string _passName;
|
||||
|
||||
public PassPopupDrawer(string passName)
|
||||
{
|
||||
_passName = passName;
|
||||
}
|
||||
|
||||
protected override void OnPropertyChange(MaterialProperty prop)
|
||||
{
|
||||
foreach (var target in prop.targets)
|
||||
{
|
||||
if (target is not Material material)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
#if UNITY_6000_1_OR_NEWER
|
||||
if (prop.propertyType != ShaderPropertyType.Int)
|
||||
#else
|
||||
if (prop.type != MaterialProperty.PropType.Int)
|
||||
#endif
|
||||
{
|
||||
material.SetShaderPassEnabled(_passName, prop.floatValue != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
material.SetShaderPassEnabled(_passName, prop.intValue != 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user