Added GetEnumValue;

Added EnumFlagsDrawer;
Added custom keywordName option to KeywordTexturePropertySingleLine;
This commit is contained in:
2025-02-03 15:44:46 +09:00
parent 4723c9833a
commit dc1f1fb4e7
6 changed files with 162 additions and 16 deletions

View File

@@ -81,9 +81,9 @@ namespace Misaki.ShaderGUI
/// <param name="label">The label of the texture property.</param> /// <param name="label">The label of the texture property.</param>
/// <param name="textureProperty">The texture property to draw.</param> /// <param name="textureProperty">The texture property to draw.</param>
/// <returns>True if the texture property is not null. Otherwise, false.</returns> /// <returns>True if the texture property is not null. Otherwise, false.</returns>
public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty) public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty, string keywordName = null)
{ {
return KeywordTexturePropertySingleLine(editor, label, textureProperty, null, null); return KeywordTexturePropertySingleLine(editor, label, textureProperty, null, null, keywordName);
} }
/// <summary> /// <summary>
@@ -94,9 +94,9 @@ namespace Misaki.ShaderGUI
/// <param name="textureProperty">The texture property to draw.</param> /// <param name="textureProperty">The texture property to draw.</param>
/// <param name="extraProperty">The first extra property to draw.</param> /// <param name="extraProperty">The first extra property to draw.</param>
/// <returns>True if the texture property is not null. Otherwise, false.</returns> /// <returns>True if the texture property is not null. Otherwise, false.</returns>
public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty, MaterialProperty extraProperty) public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty, MaterialProperty extraProperty, string keywordName = null)
{ {
return KeywordTexturePropertySingleLine(editor, label, textureProperty, extraProperty, null); return KeywordTexturePropertySingleLine(editor, label, textureProperty, extraProperty, null, keywordName);
} }
/// <summary> /// <summary>
@@ -108,13 +108,13 @@ namespace Misaki.ShaderGUI
/// <param name="extraProperty1">The first extra property to draw.</param> /// <param name="extraProperty1">The first extra property to draw.</param>
/// <param name="extraProperty2">The second extra property to draw.</param> /// <param name="extraProperty2">The second extra property to draw.</param>
/// <returns>True if the texture property is not null. Otherwise, false.</returns> /// <returns>True if the texture property is not null. Otherwise, false.</returns>
public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty, MaterialProperty extraProperty1, MaterialProperty extraProperty2) public static bool KeywordTexturePropertySingleLine(this MaterialEditor editor, GUIContent label, MaterialProperty textureProperty, MaterialProperty extraProperty1, MaterialProperty extraProperty2, string keywordName = null)
{ {
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
editor.TexturePropertySingleLine(label, textureProperty, extraProperty1, extraProperty2); editor.TexturePropertySingleLine(label, textureProperty, extraProperty1, extraProperty2);
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck())
{ {
editor.SetKeyword(textureProperty.name.ToUpper(), textureProperty.textureValue != null); editor.SetKeyword(string.IsNullOrEmpty(keywordName) ? textureProperty.name.ToUpper() : keywordName, textureProperty.textureValue != null);
} }
return textureProperty.textureValue != null; return textureProperty.textureValue != null;

View File

@@ -28,5 +28,28 @@ namespace Misaki.ShaderGUI
throw new NotSupportedException("Property type is not supported."); throw new NotSupportedException("Property type is not supported.");
} }
public static T GetEnumValue<T>(this MaterialProperty materialProperty) where T : Enum
{
#if UNITY_6000_1_OR_NEWER
if (materialProperty.propertyType == UnityEngine.Rendering.ShaderPropertyType.Float || materialProperty.propertyType == UnityEngine.Rendering.ShaderPropertyType.Range)
#else
if (materialProperty.type == MaterialProperty.PropType.Float || materialProperty.type == MaterialProperty.PropType.Range)
#endif
{
return (T)Enum.ToObject(typeof(T), (int)materialProperty.floatValue);
}
#if UNITY_6000_1_OR_NEWER
if (materialProperty.propertyType == UnityEngine.Rendering.ShaderPropertyType.Int)
#else
if (materialProperty.type == MaterialProperty.PropType.Int)
#endif
{
return (T)Enum.ToObject(typeof(T), materialProperty.intValue);
}
throw new NotSupportedException("Property type is not supported.");
}
} }
} }

View File

@@ -8,7 +8,7 @@ namespace Misaki.ShaderGUI.Packages
{ {
public abstract class MaterialUIBlock<TExpandableBit> : IMaterialUIScope where TExpandableBit : Enum, IConvertible public abstract class MaterialUIBlock<TExpandableBit> : IMaterialUIScope where TExpandableBit : Enum, IConvertible
{ {
private readonly MaterialPropertyContainer _propertyContainer = new(); protected readonly MaterialPropertyContainer propertyContainer = new();
protected MaterialEditor editor; protected MaterialEditor editor;
protected IMaterialUIScopeContainer owner; protected IMaterialUIScopeContainer owner;
@@ -39,13 +39,13 @@ namespace Misaki.ShaderGUI.Packages
editor = materialEditor; editor = materialEditor;
owner = container; owner = container;
LoadMaterialProperties(_propertyContainer); LoadMaterialProperties(propertyContainer);
UpdateMaterialProperties(properties); UpdateMaterialProperties(properties);
} }
public void UpdateMaterialProperties(IEnumerable<MaterialProperty> properties) public void UpdateMaterialProperties(IEnumerable<MaterialProperty> properties)
{ {
_propertyContainer.ReloadProperties(properties); propertyContainer.ReloadProperties(properties);
} }
public void Draw() public void Draw()

View 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();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 44cf7877640af1d4795a4d087824f9f5

View File

@@ -9,11 +9,11 @@ namespace Misaki.ShaderGUI
{ {
private static readonly GUIContent[] _options = { new("Disabled"), new("Enabled") }; 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 #if UNITY_6000_1_OR_NEWER
return prop.propertyType == ShaderPropertyType.Float || prop.propertyType == ShaderPropertyType.Range || prop.propertyType == ShaderPropertyType.Int; return prop.propertyType == ShaderPropertyType.Float || prop.propertyType == ShaderPropertyType.Range || prop.propertyType == ShaderPropertyType.Int;
@@ -41,14 +41,14 @@ namespace Misaki.ShaderGUI
{ {
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
var value = (int)Math.Abs(prop.floatValue); var value = (int)Mathf.Abs(prop.floatValue);
EditorGUI.showMixedValue = prop.hasMixedValue; EditorGUI.showMixedValue = prop.hasMixedValue;
value = EditorGUI.Popup(position, label, value, _options); value = EditorGUI.Popup(position, label, value, _options);
EditorGUI.showMixedValue = false; EditorGUI.showMixedValue = false;
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck())
{ {
prop.floatValue = value; prop.floatValue = value;
SetKeyword(prop, value != 0); OnPropertyChange(prop);
} }
} }
else else
@@ -62,7 +62,7 @@ namespace Misaki.ShaderGUI
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck())
{ {
prop.intValue = value; prop.intValue = value;
SetKeyword(prop, value != 0); OnPropertyChange(prop);
} }
} }
@@ -74,7 +74,7 @@ namespace Misaki.ShaderGUI
{ {
private const string Keyword_Suffix = "_ON"; 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; var keywordName = prop.name.ToUpper() + Keyword_Suffix;
foreach (var target in prop.targets) foreach (var target in prop.targets)
@@ -84,7 +84,52 @@ namespace Misaki.ShaderGUI
continue; 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);
}
} }
} }
} }