From 1be0cd45db952d05e51d508c58f65a6541df9de0 Mon Sep 17 00:00:00 2001 From: fumiko <2394425147@qq.com> Date: Tue, 7 Jan 2025 16:12:24 +0900 Subject: [PATCH] feat: add `Use HDR Color Picker` option for project settings (editor) close #290 --- Packages/src/Editor/UIEffectEditor.cs | 72 ++++++++++++++++--- .../Editor/UIEffectProjectSettingsEditor.cs | 10 +++ Packages/src/Runtime/UIEffect.cs | 6 -- .../src/Runtime/UIEffectProjectSettings.cs | 10 +++ 4 files changed, 83 insertions(+), 15 deletions(-) diff --git a/Packages/src/Editor/UIEffectEditor.cs b/Packages/src/Editor/UIEffectEditor.cs index dc353e3d..6f939308 100644 --- a/Packages/src/Editor/UIEffectEditor.cs +++ b/Packages/src/Editor/UIEffectEditor.cs @@ -2,6 +2,7 @@ using UnityEngine; using System.Linq; using System; +using System.Reflection; using Object = UnityEngine.Object; namespace Coffee.UIEffects.Editors @@ -13,6 +14,17 @@ namespace Coffee.UIEffects.Editors [CanEditMultipleObjects] public class UIEffect2Editor : Editor { + private static readonly PropertyInfo s_PiGradient = typeof(SerializedProperty) + .GetProperty("gradientValue", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + + private static readonly Func s_GetGradient = + (Func)Delegate.CreateDelegate(typeof(Func), + s_PiGradient.GetMethod); + + private static readonly Action s_SetGradient = + (Action)Delegate.CreateDelegate(typeof(Action), + s_PiGradient.SetMethod); + private SerializedProperty _toneFilter; private SerializedProperty _toneIntensity; @@ -162,7 +174,7 @@ public void DrawProperties() { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(_colorIntensity); - DrawColor(_colorFilter, _color, prevColorFilter); + DrawColor(_colorFilter, _color, prevColorFilter, false); EditorGUILayout.PropertyField(_colorGlow); EditorGUI.indentLevel--; } @@ -260,7 +272,7 @@ public void DrawProperties() } EditorGUILayout.PropertyField(_shadowColorFilter); - EditorGUILayout.PropertyField(_shadowColor); + DrawColorPickerField(_shadowColor, false); EditorGUILayout.PropertyField(_shadowColorGlow); EditorGUILayout.PropertyField(_shadowFade); @@ -286,15 +298,16 @@ public void DrawProperties() case GradationMode.HorizontalGradient: case GradationMode.VerticalGradient: case GradationMode.AngleGradient: - EditorGUILayout.PropertyField(_gradationGradient); + DrawGradientField(_gradationGradient); break; default: - EditorGUILayout.PropertyField(_gradationColor1); + DrawColorPickerField(_gradationColor1); var r = EditorGUILayout.GetControlRect(); - r.width -= 20; - EditorGUI.PropertyField(r, _gradationColor2); + r.width -= 24; + r.height = EditorGUIUtility.singleLineHeight; + DrawColorPickerField(r, _gradationColor2); - r.x += r.width; + r.x += r.width + 4; r.width = 20; // Swap colors if (GUI.Button(r, EditorGUIUtility.IconContent("preaudioloopoff"), "iconbutton")) @@ -326,7 +339,48 @@ public void DrawProperties() } } - private static void DrawColor(SerializedProperty filter, SerializedProperty color, ColorFilter prevFilter) + private static void DrawColorPickerField(SerializedProperty color, bool showAlpha = true) + { + var r = EditorGUILayout.GetControlRect(); + r.height = EditorGUIUtility.singleLineHeight; + DrawColorPickerField(r, color, showAlpha); + } + + private static void DrawColorPickerField(Rect rect, SerializedProperty color, bool showAlpha = true) + { + var label = EditorGUIUtility.TrTempContent(color.displayName); + label.tooltip = color.tooltip; + var hdr = UIEffectProjectSettings.useHdrColorPicker; + EditorGUI.showMixedValue = color.hasMultipleDifferentValues; + + EditorGUI.BeginChangeCheck(); + var colorValue = EditorGUI.ColorField(rect, label, color.colorValue, true, showAlpha, hdr); + if (EditorGUI.EndChangeCheck()) + { + color.colorValue = colorValue; + } + } + + private static void DrawGradientField(SerializedProperty gradient) + { + var r = EditorGUILayout.GetControlRect(); + r.height = EditorGUIUtility.singleLineHeight; + + var label = EditorGUIUtility.TrTempContent(gradient.displayName); + label.tooltip = gradient.tooltip; + var hdr = UIEffectProjectSettings.useHdrColorPicker; + EditorGUI.showMixedValue = gradient.hasMultipleDifferentValues; + + EditorGUI.BeginChangeCheck(); + var gradientValue = EditorGUI.GradientField(r, label, s_GetGradient(gradient), hdr); + if (EditorGUI.EndChangeCheck()) + { + s_SetGradient(gradient, gradientValue); + } + } + + private static void DrawColor(SerializedProperty filter, SerializedProperty color, ColorFilter prevFilter, + bool showAlpha = true) { if (filter.intValue == (int)ColorFilter.None) { @@ -361,7 +415,7 @@ private static void DrawColor(SerializedProperty filter, SerializedProperty colo color.colorValue = Color.white; } - EditorGUILayout.PropertyField(color); + DrawColorPickerField(color, showAlpha); } } diff --git a/Packages/src/Editor/UIEffectProjectSettingsEditor.cs b/Packages/src/Editor/UIEffectProjectSettingsEditor.cs index 04c1b1ff..1af583f1 100644 --- a/Packages/src/Editor/UIEffectProjectSettingsEditor.cs +++ b/Packages/src/Editor/UIEffectProjectSettingsEditor.cs @@ -9,6 +9,7 @@ namespace Coffee.UIEffects.Editors public class UIEffectProjectSettingsEditor : Editor { private ReorderableList _reorderableList; + private SerializedProperty _useHDRColorPicker; private SerializedProperty _transformSensitivity; private bool _isInitialized; private ShaderVariantRegistryEditor _shaderVariantRegistryEditor; @@ -18,6 +19,7 @@ private void InitializeIfNeeded() if (_isInitialized) return; _transformSensitivity = serializedObject.FindProperty("m_TransformSensitivity"); + _useHDRColorPicker = serializedObject.FindProperty("m_UseHDRColorPicker"); var runtimePresets = serializedObject.FindProperty("m_RuntimePresets"); _reorderableList = new ReorderableList(serializedObject, runtimePresets, true, true, true, true); _reorderableList.drawHeaderCallback = rect => EditorGUI.LabelField(rect, "Runtime Presets"); @@ -63,9 +65,17 @@ public override void OnInspectorGUI() InitializeIfNeeded(); // Settings + // Transform sensitivity. EditorGUILayout.PropertyField(_transformSensitivity); + + // Runtime Presets _reorderableList.DoLayoutList(); + // Editor + // Use HDR color pickers. + EditorGUILayout.PropertyField(_useHDRColorPicker); + + // Shader // Shader registry EditorGUILayout.Space(); EditorGUILayout.LabelField("Shader", EditorStyles.boldLabel); diff --git a/Packages/src/Runtime/UIEffect.cs b/Packages/src/Runtime/UIEffect.cs index 3c20bd0e..b0bb19cb 100644 --- a/Packages/src/Runtime/UIEffect.cs +++ b/Packages/src/Runtime/UIEffect.cs @@ -26,7 +26,6 @@ public class UIEffect : UIEffectBase [SerializeField] protected float m_ColorIntensity = 1; - [ColorUsage(false, true)] [SerializeField] protected Color m_Color = Color.white; @@ -81,7 +80,6 @@ public class UIEffect : UIEffectBase [SerializeField] protected ColorFilter m_TransitionColorFilter = ColorFilter.MultiplyAdditive; - [ColorUsage(true, true)] [SerializeField] protected Color m_TransitionColor = new Color(0f, 0.5f, 1.0f, 1.0f); @@ -91,7 +89,6 @@ public class UIEffect : UIEffectBase [SerializeField] protected TargetMode m_TargetMode = TargetMode.None; - [ColorUsage(false, false)] [SerializeField] protected Color m_TargetColor = Color.white; @@ -137,7 +134,6 @@ public class UIEffect : UIEffectBase [SerializeField] protected ColorFilter m_ShadowColorFilter = ColorFilter.Replace; - [ColorUsage(false, true)] [SerializeField] protected Color m_ShadowColor = Color.white; @@ -148,11 +144,9 @@ public class UIEffect : UIEffectBase protected GradationMode m_GradationMode = GradationMode.None; [SerializeField] - [ColorUsage(true, true)] protected Color m_GradationColor1 = Color.white; [SerializeField] - [ColorUsage(true, true)] protected Color m_GradationColor2 = Color.white; [SerializeField] diff --git a/Packages/src/Runtime/UIEffectProjectSettings.cs b/Packages/src/Runtime/UIEffectProjectSettings.cs index 4bc34fce..10473689 100644 --- a/Packages/src/Runtime/UIEffectProjectSettings.cs +++ b/Packages/src/Runtime/UIEffectProjectSettings.cs @@ -20,6 +20,10 @@ public class UIEffectProjectSettings : PreloadedProjectSettings m_RuntimePresets = new List(); + [Header("Editor")] + [Tooltip("Use HDR color pickers on color fields.")] + [SerializeField] private bool m_UseHDRColorPicker = true; + [HideInInspector] [SerializeField] internal ShaderVariantCollection m_ShaderVariantCollection; @@ -38,6 +42,12 @@ public static TransformSensitivity transformSensitivity set => instance.m_TransformSensitivity = value; } + public static bool useHdrColorPicker + { + get => instance.m_UseHDRColorPicker; + set => instance.m_UseHDRColorPicker = value; + } + public static void RegisterRuntimePreset(UIEffect effect) { // Already registered.