Skip to content

Commit

Permalink
feat: use m_UseHdrColorPicker field inside UIEffectProjectSettings.…
Browse files Browse the repository at this point in the history
…cs with custom color picker fields instead of using a scripting define
  • Loading branch information
2394425147 authored and mob-sakai committed Jan 7, 2025
1 parent 963f54e commit f504975
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 74 deletions.
30 changes: 24 additions & 6 deletions Packages/src/Editor/UIEffectEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void DrawProperties()
}

EditorGUILayout.PropertyField(_shadowColorFilter);
EditorGUILayout.PropertyField(_shadowColor);
DrawColorPickerField(_shadowColor, false);
EditorGUILayout.PropertyField(_shadowColorGlow);
EditorGUILayout.PropertyField(_shadowFade);

Expand Down Expand Up @@ -289,12 +289,14 @@ public void DrawProperties()
EditorGUILayout.PropertyField(_gradationGradient);
break;
default:
EditorGUILayout.PropertyField(_gradationColor1);
var r = EditorGUILayout.GetControlRect();
r.width -= 20;
EditorGUI.PropertyField(r, _gradationColor2);
r.height = EditorGUIUtility.singleLineHeight;
DrawColorPickerField(_gradationColor1, r, new GUIContent("Gradation Color 1"));
r = EditorGUILayout.GetControlRect();
r.width -= 24;
DrawColorPickerField(_gradationColor2, r, new GUIContent("Gradation Color 2"));

r.x += r.width;
r.x += r.width + 4;
r.width = 20;
// Swap colors
if (GUI.Button(r, EditorGUIUtility.IconContent("preaudioloopoff"), "iconbutton"))
Expand Down Expand Up @@ -326,6 +328,22 @@ public void DrawProperties()
}
}

private static void DrawColorPickerField(SerializedProperty color, bool showAlpha = true)
{
var r = EditorGUILayout.GetControlRect();
r.height = EditorGUIUtility.singleLineHeight;
DrawColorPickerField(color, r, new GUIContent(color.displayName, color.tooltip), showAlpha);
}

private static void DrawColorPickerField(SerializedProperty color, Rect rect, GUIContent label, bool showAlpha = true)
{
EditorGUI.BeginChangeCheck();
EditorGUI.showMixedValue = color.hasMultipleDifferentValues;
var colorField = EditorGUI.ColorField(rect, label, color.colorValue, true, showAlpha, UIEffectProjectSettings.useHdrColorPicker);
if (EditorGUI.EndChangeCheck())
color.colorValue = colorField;
}

private static void DrawColor(SerializedProperty filter, SerializedProperty color, ColorFilter prevFilter)
{
if (filter.intValue == (int)ColorFilter.None)
Expand Down Expand Up @@ -361,7 +379,7 @@ private static void DrawColor(SerializedProperty filter, SerializedProperty colo
color.colorValue = Color.white;
}

EditorGUILayout.PropertyField(color);
DrawColorPickerField(color);
}
}

Expand Down
63 changes: 5 additions & 58 deletions Packages/src/Editor/UIEffectProjectSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,23 @@
using Coffee.UIEffectInternal;
using UnityEditorInternal;

#if UNITY_2021_3_OR_NEWER
using UnityEditor.Build;
#endif

namespace Coffee.UIEffects.Editors
{
[CustomEditor(typeof(UIEffectProjectSettings))]
public class UIEffectProjectSettingsEditor : Editor
{
private const string k_NoHDRGradientScriptingDefine = "UIEFFECTS_GRADIENT_NO_HDR";

private bool _isInitialized;
private ReorderableList _reorderableList;
private bool _noHdrGradient;
private SerializedProperty _useHdrColorPicker;
private SerializedProperty _transformSensitivity;
private bool _isInitialized;
private ShaderVariantRegistryEditor _shaderVariantRegistryEditor;

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");
Expand Down Expand Up @@ -65,18 +60,6 @@ private void OnDisable()
_isInitialized = false;
}

private void Awake()
{
// Called when the domain reloads,
// So we check if the scripting define is altered manually
#if UNITY_2021_3_OR_NEWER
PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(EditorUserBuildSettings.selectedBuildTargetGroup), out var defines);
#else
PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, out var defines);
#endif
_noHdrGradient = Array.IndexOf(defines, k_NoHDRGradientScriptingDefine) != -1;
}

public override void OnInspectorGUI()
{
serializedObject.Update();
Expand All @@ -85,45 +68,9 @@ public override void OnInspectorGUI()
// Settings
EditorGUILayout.PropertyField(_transformSensitivity);

var useHdrGradient = !_noHdrGradient;
if (EditorGUILayout.Toggle(new GUIContent("HDR Gradient", "Use HDR colors on two-color gradients"), useHdrGradient) != useHdrGradient)
{
_noHdrGradient = !_noHdrGradient;
if (_noHdrGradient)
{
#if UNITY_2021_3_OR_NEWER
PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(EditorUserBuildSettings.selectedBuildTargetGroup), out var defines);
#else
PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, out var defines);
#endif

Array.Resize(ref defines, defines.Length + 1);
defines[defines.Length - 1] = k_NoHDRGradientScriptingDefine;

#if UNITY_2021_3_OR_NEWER
PlayerSettings.SetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(EditorUserBuildSettings.selectedBuildTargetGroup), defines);
#else
PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
#endif
}
else
{
#if UNITY_2021_3_OR_NEWER
PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(EditorUserBuildSettings.selectedBuildTargetGroup), out var defines);
#else
PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, out var defines);
#endif

defines[Array.IndexOf(defines, k_NoHDRGradientScriptingDefine)] = defines[defines.Length - 1];
Array.Resize(ref defines, defines.Length - 1);
// A GUIContent is used here to override the capitalization of HDR
EditorGUILayout.PropertyField(_useHdrColorPicker, new GUIContent("HDR Color Picker", "Use HDR color pickers on color fields."));

#if UNITY_2021_3_OR_NEWER
PlayerSettings.SetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(EditorUserBuildSettings.selectedBuildTargetGroup), defines);
#else
PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
#endif
}
}
_reorderableList.DoLayoutList();

// Shader registry
Expand Down
8 changes: 0 additions & 8 deletions Packages/src/Runtime/UIEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,11 @@ public class UIEffect : UIEffectBase
protected GradationMode m_GradationMode = GradationMode.None;

[SerializeField]
#if UIEFFECTS_GRADIENT_NO_HDR
[ColorUsage(true)]
#else
[ColorUsage(true, true)]
#endif
protected Color m_GradationColor1 = Color.white;

[SerializeField]
#if UIEFFECTS_GRADIENT_NO_HDR
[ColorUsage(true)]
#else
[ColorUsage(true, true)]
#endif
protected Color m_GradationColor2 = Color.white;

[SerializeField]
Expand Down
12 changes: 10 additions & 2 deletions Packages/src/Runtime/UIEffectProjectSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -17,6 +16,9 @@ public class UIEffectProjectSettings : PreloadedProjectSettings<UIEffectProjectS
[SerializeField]
private TransformSensitivity m_TransformSensitivity = TransformSensitivity.Medium;

[Tooltip("Use HDR color pickers on color fields.")]
[SerializeField] private bool m_UseHdrColorPicker = true;

[SerializeField]
internal List<UIEffect> m_RuntimePresets = new List<UIEffect>();

Expand All @@ -38,6 +40,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.
Expand Down Expand Up @@ -113,7 +121,7 @@ private void Refresh()
m_ShaderVariantRegistry.ClearCache();
MaterialRepository.Clear();
foreach (var c in Misc.FindObjectsOfType<UIEffectBase>()
.Concat(Misc.GetAllComponentsInPrefabStage<UIEffectBase>()))
.Concat(Misc.GetAllComponentsInPrefabStage<UIEffectBase>()))
{
c.SetMaterialDirty();
}
Expand Down

0 comments on commit f504975

Please sign in to comment.