Skip to content

Commit 1be0cd4

Browse files
2394425147mob-sakai
authored andcommitted
feat: add Use HDR Color Picker option for project settings (editor)
close #290
1 parent 9799069 commit 1be0cd4

File tree

4 files changed

+83
-15
lines changed

4 files changed

+83
-15
lines changed

Packages/src/Editor/UIEffectEditor.cs

+63-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using UnityEngine;
33
using System.Linq;
44
using System;
5+
using System.Reflection;
56
using Object = UnityEngine.Object;
67

78
namespace Coffee.UIEffects.Editors
@@ -13,6 +14,17 @@ namespace Coffee.UIEffects.Editors
1314
[CanEditMultipleObjects]
1415
public class UIEffect2Editor : Editor
1516
{
17+
private static readonly PropertyInfo s_PiGradient = typeof(SerializedProperty)
18+
.GetProperty("gradientValue", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
19+
20+
private static readonly Func<SerializedProperty, Gradient> s_GetGradient =
21+
(Func<SerializedProperty, Gradient>)Delegate.CreateDelegate(typeof(Func<SerializedProperty, Gradient>),
22+
s_PiGradient.GetMethod);
23+
24+
private static readonly Action<SerializedProperty, Gradient> s_SetGradient =
25+
(Action<SerializedProperty, Gradient>)Delegate.CreateDelegate(typeof(Action<SerializedProperty, Gradient>),
26+
s_PiGradient.SetMethod);
27+
1628
private SerializedProperty _toneFilter;
1729
private SerializedProperty _toneIntensity;
1830

@@ -162,7 +174,7 @@ public void DrawProperties()
162174
{
163175
EditorGUI.indentLevel++;
164176
EditorGUILayout.PropertyField(_colorIntensity);
165-
DrawColor(_colorFilter, _color, prevColorFilter);
177+
DrawColor(_colorFilter, _color, prevColorFilter, false);
166178
EditorGUILayout.PropertyField(_colorGlow);
167179
EditorGUI.indentLevel--;
168180
}
@@ -260,7 +272,7 @@ public void DrawProperties()
260272
}
261273

262274
EditorGUILayout.PropertyField(_shadowColorFilter);
263-
EditorGUILayout.PropertyField(_shadowColor);
275+
DrawColorPickerField(_shadowColor, false);
264276
EditorGUILayout.PropertyField(_shadowColorGlow);
265277
EditorGUILayout.PropertyField(_shadowFade);
266278

@@ -286,15 +298,16 @@ public void DrawProperties()
286298
case GradationMode.HorizontalGradient:
287299
case GradationMode.VerticalGradient:
288300
case GradationMode.AngleGradient:
289-
EditorGUILayout.PropertyField(_gradationGradient);
301+
DrawGradientField(_gradationGradient);
290302
break;
291303
default:
292-
EditorGUILayout.PropertyField(_gradationColor1);
304+
DrawColorPickerField(_gradationColor1);
293305
var r = EditorGUILayout.GetControlRect();
294-
r.width -= 20;
295-
EditorGUI.PropertyField(r, _gradationColor2);
306+
r.width -= 24;
307+
r.height = EditorGUIUtility.singleLineHeight;
308+
DrawColorPickerField(r, _gradationColor2);
296309

297-
r.x += r.width;
310+
r.x += r.width + 4;
298311
r.width = 20;
299312
// Swap colors
300313
if (GUI.Button(r, EditorGUIUtility.IconContent("preaudioloopoff"), "iconbutton"))
@@ -326,7 +339,48 @@ public void DrawProperties()
326339
}
327340
}
328341

329-
private static void DrawColor(SerializedProperty filter, SerializedProperty color, ColorFilter prevFilter)
342+
private static void DrawColorPickerField(SerializedProperty color, bool showAlpha = true)
343+
{
344+
var r = EditorGUILayout.GetControlRect();
345+
r.height = EditorGUIUtility.singleLineHeight;
346+
DrawColorPickerField(r, color, showAlpha);
347+
}
348+
349+
private static void DrawColorPickerField(Rect rect, SerializedProperty color, bool showAlpha = true)
350+
{
351+
var label = EditorGUIUtility.TrTempContent(color.displayName);
352+
label.tooltip = color.tooltip;
353+
var hdr = UIEffectProjectSettings.useHdrColorPicker;
354+
EditorGUI.showMixedValue = color.hasMultipleDifferentValues;
355+
356+
EditorGUI.BeginChangeCheck();
357+
var colorValue = EditorGUI.ColorField(rect, label, color.colorValue, true, showAlpha, hdr);
358+
if (EditorGUI.EndChangeCheck())
359+
{
360+
color.colorValue = colorValue;
361+
}
362+
}
363+
364+
private static void DrawGradientField(SerializedProperty gradient)
365+
{
366+
var r = EditorGUILayout.GetControlRect();
367+
r.height = EditorGUIUtility.singleLineHeight;
368+
369+
var label = EditorGUIUtility.TrTempContent(gradient.displayName);
370+
label.tooltip = gradient.tooltip;
371+
var hdr = UIEffectProjectSettings.useHdrColorPicker;
372+
EditorGUI.showMixedValue = gradient.hasMultipleDifferentValues;
373+
374+
EditorGUI.BeginChangeCheck();
375+
var gradientValue = EditorGUI.GradientField(r, label, s_GetGradient(gradient), hdr);
376+
if (EditorGUI.EndChangeCheck())
377+
{
378+
s_SetGradient(gradient, gradientValue);
379+
}
380+
}
381+
382+
private static void DrawColor(SerializedProperty filter, SerializedProperty color, ColorFilter prevFilter,
383+
bool showAlpha = true)
330384
{
331385
if (filter.intValue == (int)ColorFilter.None)
332386
{
@@ -361,7 +415,7 @@ private static void DrawColor(SerializedProperty filter, SerializedProperty colo
361415
color.colorValue = Color.white;
362416
}
363417

364-
EditorGUILayout.PropertyField(color);
418+
DrawColorPickerField(color, showAlpha);
365419
}
366420
}
367421

Packages/src/Editor/UIEffectProjectSettingsEditor.cs

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Coffee.UIEffects.Editors
99
public class UIEffectProjectSettingsEditor : Editor
1010
{
1111
private ReorderableList _reorderableList;
12+
private SerializedProperty _useHDRColorPicker;
1213
private SerializedProperty _transformSensitivity;
1314
private bool _isInitialized;
1415
private ShaderVariantRegistryEditor _shaderVariantRegistryEditor;
@@ -18,6 +19,7 @@ private void InitializeIfNeeded()
1819
if (_isInitialized) return;
1920

2021
_transformSensitivity = serializedObject.FindProperty("m_TransformSensitivity");
22+
_useHDRColorPicker = serializedObject.FindProperty("m_UseHDRColorPicker");
2123
var runtimePresets = serializedObject.FindProperty("m_RuntimePresets");
2224
_reorderableList = new ReorderableList(serializedObject, runtimePresets, true, true, true, true);
2325
_reorderableList.drawHeaderCallback = rect => EditorGUI.LabelField(rect, "Runtime Presets");
@@ -63,9 +65,17 @@ public override void OnInspectorGUI()
6365
InitializeIfNeeded();
6466

6567
// Settings
68+
// Transform sensitivity.
6669
EditorGUILayout.PropertyField(_transformSensitivity);
70+
71+
// Runtime Presets
6772
_reorderableList.DoLayoutList();
6873

74+
// Editor
75+
// Use HDR color pickers.
76+
EditorGUILayout.PropertyField(_useHDRColorPicker);
77+
78+
// Shader
6979
// Shader registry
7080
EditorGUILayout.Space();
7181
EditorGUILayout.LabelField("Shader", EditorStyles.boldLabel);

Packages/src/Runtime/UIEffect.cs

-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class UIEffect : UIEffectBase
2626
[SerializeField]
2727
protected float m_ColorIntensity = 1;
2828

29-
[ColorUsage(false, true)]
3029
[SerializeField]
3130
protected Color m_Color = Color.white;
3231

@@ -81,7 +80,6 @@ public class UIEffect : UIEffectBase
8180
[SerializeField]
8281
protected ColorFilter m_TransitionColorFilter = ColorFilter.MultiplyAdditive;
8382

84-
[ColorUsage(true, true)]
8583
[SerializeField]
8684
protected Color m_TransitionColor = new Color(0f, 0.5f, 1.0f, 1.0f);
8785

@@ -91,7 +89,6 @@ public class UIEffect : UIEffectBase
9189
[SerializeField]
9290
protected TargetMode m_TargetMode = TargetMode.None;
9391

94-
[ColorUsage(false, false)]
9592
[SerializeField]
9693
protected Color m_TargetColor = Color.white;
9794

@@ -137,7 +134,6 @@ public class UIEffect : UIEffectBase
137134
[SerializeField]
138135
protected ColorFilter m_ShadowColorFilter = ColorFilter.Replace;
139136

140-
[ColorUsage(false, true)]
141137
[SerializeField]
142138
protected Color m_ShadowColor = Color.white;
143139

@@ -148,11 +144,9 @@ public class UIEffect : UIEffectBase
148144
protected GradationMode m_GradationMode = GradationMode.None;
149145

150146
[SerializeField]
151-
[ColorUsage(true, true)]
152147
protected Color m_GradationColor1 = Color.white;
153148

154149
[SerializeField]
155-
[ColorUsage(true, true)]
156150
protected Color m_GradationColor2 = Color.white;
157151

158152
[SerializeField]

Packages/src/Runtime/UIEffectProjectSettings.cs

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class UIEffectProjectSettings : PreloadedProjectSettings<UIEffectProjectS
2020
[SerializeField]
2121
internal List<UIEffect> m_RuntimePresets = new List<UIEffect>();
2222

23+
[Header("Editor")]
24+
[Tooltip("Use HDR color pickers on color fields.")]
25+
[SerializeField] private bool m_UseHDRColorPicker = true;
26+
2327
[HideInInspector]
2428
[SerializeField]
2529
internal ShaderVariantCollection m_ShaderVariantCollection;
@@ -38,6 +42,12 @@ public static TransformSensitivity transformSensitivity
3842
set => instance.m_TransformSensitivity = value;
3943
}
4044

45+
public static bool useHdrColorPicker
46+
{
47+
get => instance.m_UseHDRColorPicker;
48+
set => instance.m_UseHDRColorPicker = value;
49+
}
50+
4151
public static void RegisterRuntimePreset(UIEffect effect)
4252
{
4353
// Already registered.

0 commit comments

Comments
 (0)