Initial upload
This commit is contained in:
91
Editor/PropertyDrawer/PopupDrawer.cs
Normal file
91
Editor/PropertyDrawer/PopupDrawer.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace Misaki.ShaderGUI
|
||||
{
|
||||
public class PopupUIDrawer : MaterialPropertyDrawer
|
||||
{
|
||||
private static readonly GUIContent[] _options = { new("Disabled"), new("Enabled") };
|
||||
|
||||
protected virtual void SetKeyword(MaterialProperty prop, bool on)
|
||||
{
|
||||
}
|
||||
|
||||
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)Math.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);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
var value = Math.Abs(prop.intValue);
|
||||
EditorGUI.showMixedValue = prop.hasMixedValue;
|
||||
value = EditorGUI.Popup(position, label, value, _options);
|
||||
EditorGUI.showMixedValue = false;
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
prop.intValue = value;
|
||||
SetKeyword(prop, value != 0);
|
||||
}
|
||||
}
|
||||
|
||||
MaterialEditor.EndProperty();
|
||||
}
|
||||
}
|
||||
|
||||
public class PopupDrawer : PopupUIDrawer
|
||||
{
|
||||
private const string Keyword_Suffix = "_ON";
|
||||
|
||||
protected override void SetKeyword(MaterialProperty prop, bool on)
|
||||
{
|
||||
var keywordName = prop.name.ToUpper() + Keyword_Suffix;
|
||||
foreach (var target in prop.targets)
|
||||
{
|
||||
if (target is not Material material)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
material.SetKeyword(new LocalKeyword(material.shader, keywordName), on);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Editor/PropertyDrawer/PopupDrawer.cs.meta
Normal file
2
Editor/PropertyDrawer/PopupDrawer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bdbc6f7a1b3b5d4489e5ed18fd3c1202
|
||||
Reference in New Issue
Block a user