Skip to content

Commit a61fc6d

Browse files
committed
Optimize property selection menu
1 parent 1995c2f commit a61fc6d

27 files changed

+529
-234
lines changed

Editor/Script/BaseBinderEditor.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ protected void DrawDataKey(SerializedProperty property)
3434

3535
protected void DrawDirection(SerializedProperty property)
3636
{
37-
EditorUtil.DrawToolbarEnum(property, typeof(DataDirection));
37+
GUIUtil.DrawToolbarEnum(property, typeof(DataDirection));
3838
}
3939

4040
protected void DrawTarget<TComponent>(SerializedProperty property, Transform root) where TComponent : Component
4141
{
42-
EditorUtil.ComponentTreeMenu<TComponent>("Target", property, root);
42+
GUIMenu.ComponentTreeMenu<TComponent>("Target", property, root);
4343
}
4444

4545
public static void DrawTargetAndProperty<TComponent>(string targetPropertyName, SerializedProperty targetProperty, Transform parent, string propertyName, SerializedProperty property) where TComponent : Component
4646
{
4747
var originalTarget = targetProperty.objectReferenceValue;
48-
EditorUtil.ComponentTreeMenu<TComponent>(targetPropertyName, targetProperty, parent, component =>
48+
GUIMenu.ComponentTreeMenu<TComponent>(targetPropertyName, targetProperty, parent, component =>
4949
{
5050
// Auto Bind
5151
var change = originalTarget != targetProperty.objectReferenceValue && targetProperty.objectReferenceValue != null;
@@ -65,7 +65,7 @@ public static void DrawTargetAndProperty<TComponent>(string targetPropertyName,
6565
if (targetProperty.objectReferenceValue != null)
6666
{
6767
var type = targetProperty.objectReferenceValue.GetType();
68-
EditorUtil.PropertyTreeMenu(propertyName, type, property);
68+
GUIMenu.DrawPropertyMenu(null, targetProperty.objectReferenceValue.GetType(), propertyName, property);
6969
}
7070
}
7171
}

Editor/Script/Util.meta renamed to Editor/Script/GUI.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Script/GUI/EditorIcon.cs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#if UNITY_EDITOR
2+
using System;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace Aya.DataBinding
7+
{
8+
public static class EditorIcon
9+
{
10+
#region Icon Method
11+
12+
public static Texture2D CreateIcon(Type type, int size = 24)
13+
{
14+
var srcIcon = GetIcon(type);
15+
if (srcIcon == null) return default;
16+
var icon = CreateIconWithSrc(srcIcon, size);
17+
return icon;
18+
}
19+
20+
public static Texture2D CreateIcon(string name, int size = 24)
21+
{
22+
var srcIcon = GetIcon(name);
23+
if (srcIcon == null) return default;
24+
var icon = CreateIconWithSrc(srcIcon, size);
25+
return icon;
26+
}
27+
28+
public static Texture2D GetIcon(Type type)
29+
{
30+
if (type != null) return EditorGUIUtility.ObjectContent(null, type).image as Texture2D;
31+
return default;
32+
}
33+
34+
public static Texture2D GetIcon(string name)
35+
{
36+
if (!string.IsNullOrEmpty(name))
37+
{
38+
var icon = EditorGUIUtility.FindTexture(name);
39+
if (icon == null) icon = AssetDatabase.GetCachedIcon(name) as Texture2D;
40+
if (icon == null) icon = EditorGUIUtility.IconContent(name).image as Texture2D;
41+
return icon;
42+
}
43+
44+
return default;
45+
}
46+
47+
public static Texture2D CreateIconWithSrc(Texture2D srcIcon, int size = 24)
48+
{
49+
// Copy Built-in texture with RenderTexture
50+
var tempRenderTexture = RenderTexture.GetTemporary(size, size, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
51+
Graphics.Blit(srcIcon, tempRenderTexture);
52+
var previousRenderTexture = RenderTexture.active;
53+
RenderTexture.active = tempRenderTexture;
54+
var icon = new Texture2D(size, size);
55+
icon.ReadPixels(new Rect(0, 0, tempRenderTexture.width, tempRenderTexture.height), 0, 0);
56+
icon.Apply();
57+
RenderTexture.ReleaseTemporary(tempRenderTexture);
58+
RenderTexture.active = previousRenderTexture;
59+
return icon;
60+
}
61+
62+
#endregion
63+
}
64+
65+
}
66+
#endif

Editor/Script/GUI/EditorIcon.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

Editor/Script/GUI/GUIMenu.cs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#if UNITY_EDITOR
2+
using System;
3+
using System.Collections.Generic;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace Aya.DataBinding
8+
{
9+
public static partial class GUIMenu
10+
{
11+
public static void DrawSearchableDropdownMenu(string propertyName,
12+
SearchableDropdown menu,
13+
Func<bool> checkNullFunc,
14+
Func<string> currentDisplayNameGetter)
15+
{
16+
var isNull = checkNullFunc();
17+
using (GUIErrorColorArea.Create(isNull))
18+
{
19+
GUILayout.BeginHorizontal();
20+
GUILayout.Label(propertyName, GUILayout.Width(EditorGUIUtility.labelWidth));
21+
22+
var displayName = currentDisplayNameGetter();
23+
var currentPropertyDisplayName = isNull ? EditorStyle.NoneStr : displayName;
24+
var btnRect = GUILayoutUtility.GetRect(new GUIContent(currentPropertyDisplayName), EditorStyles.toolbarButton);
25+
var btnType = GUI.Button(btnRect, currentPropertyDisplayName, EditorStyles.popup);
26+
if (btnType)
27+
{
28+
btnRect.width = EditorGUIUtility.currentViewWidth;
29+
menu.Show(btnRect, 500f);
30+
}
31+
32+
GUILayout.EndHorizontal();
33+
}
34+
}
35+
36+
public static SearchableDropdown CreateSearchableDropdownMenu<T>(string menuTitle,
37+
IEnumerable<T> valueSource,
38+
Func<T, string> pathGetter,
39+
Func<T, Texture2D> iconGetter,
40+
Action<T> onClick = null)
41+
{
42+
var root = new SearchableDropdownItem(menuTitle);
43+
var menu = new SearchableDropdown(root, item =>
44+
{
45+
var value = (T)item.Value;
46+
onClick?.Invoke(value);
47+
});
48+
49+
root.AddChild(new SearchableDropdownItem(EditorStyle.NoneStr, null));
50+
root.AddSeparator();
51+
52+
foreach (var value in valueSource)
53+
{
54+
var path = pathGetter(value);
55+
var icon = iconGetter(value);
56+
var item = new SearchableDropdownItem(path, value)
57+
{
58+
icon = icon
59+
};
60+
61+
root.AddChild(item);
62+
}
63+
64+
return menu;
65+
}
66+
}
67+
}
68+
#endif

Editor/Script/GUI/GUIMenu.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Script/Util/GUIStruct.cs renamed to Editor/Script/GUI/GUIStruct.cs

+24
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,29 @@ public void Dispose()
200200
GUI.color = OriginalColor;
201201
}
202202
}
203+
204+
public struct GUIErrorColorArea : IDisposable
205+
{
206+
public Color OriginalColor;
207+
208+
public static GUIErrorColorArea Create(bool check = true)
209+
{
210+
return new GUIErrorColorArea(check);
211+
}
212+
213+
public GUIErrorColorArea(bool check = true)
214+
{
215+
OriginalColor = GUI.color;
216+
if (check)
217+
{
218+
GUI.color = new Color(1f, 0.5f, 0.5f);
219+
}
220+
}
221+
222+
public void Dispose()
223+
{
224+
GUI.color = OriginalColor;
225+
}
226+
}
203227
}
204228
#endif

0 commit comments

Comments
 (0)