Files
com.misaki.shader-gui/Editor/PropertyDrawer/EnumFlagsDrawer.cs
Misaki 8250bc128d Added new KeywordEnumTypeDrawer;
Added new method to use generic to add ui scope
2025-08-17 13:01:40 +09:00

77 lines
2.6 KiB
C#

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.Int;
#endif
}
public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
if (!IsPropertyTypeSuitable(prop))
{
var c = EditorGUIUtility.TrTempContent($"Enum flags used on a property {prop.name} with unsupported type: {prop.propertyType}");
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();
}
}
}