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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user