Skip to content

Commit

Permalink
Optimize property selection menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ls9512 committed Mar 22, 2024
1 parent 1995c2f commit a61fc6d
Show file tree
Hide file tree
Showing 27 changed files with 529 additions and 234 deletions.
8 changes: 4 additions & 4 deletions Editor/Script/BaseBinderEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ protected void DrawDataKey(SerializedProperty property)

protected void DrawDirection(SerializedProperty property)
{
EditorUtil.DrawToolbarEnum(property, typeof(DataDirection));
GUIUtil.DrawToolbarEnum(property, typeof(DataDirection));
}

protected void DrawTarget<TComponent>(SerializedProperty property, Transform root) where TComponent : Component
{
EditorUtil.ComponentTreeMenu<TComponent>("Target", property, root);
GUIMenu.ComponentTreeMenu<TComponent>("Target", property, root);
}

public static void DrawTargetAndProperty<TComponent>(string targetPropertyName, SerializedProperty targetProperty, Transform parent, string propertyName, SerializedProperty property) where TComponent : Component
{
var originalTarget = targetProperty.objectReferenceValue;
EditorUtil.ComponentTreeMenu<TComponent>(targetPropertyName, targetProperty, parent, component =>
GUIMenu.ComponentTreeMenu<TComponent>(targetPropertyName, targetProperty, parent, component =>
{
// Auto Bind
var change = originalTarget != targetProperty.objectReferenceValue && targetProperty.objectReferenceValue != null;
Expand All @@ -65,7 +65,7 @@ public static void DrawTargetAndProperty<TComponent>(string targetPropertyName,
if (targetProperty.objectReferenceValue != null)
{
var type = targetProperty.objectReferenceValue.GetType();
EditorUtil.PropertyTreeMenu(propertyName, type, property);
GUIMenu.DrawPropertyMenu(null, targetProperty.objectReferenceValue.GetType(), propertyName, property);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Editor/Script/Util.meta → Editor/Script/GUI.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions Editor/Script/GUI/EditorIcon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#if UNITY_EDITOR
using System;
using UnityEditor;
using UnityEngine;

namespace Aya.DataBinding
{
public static class EditorIcon
{
#region Icon Method

public static Texture2D CreateIcon(Type type, int size = 24)
{
var srcIcon = GetIcon(type);
if (srcIcon == null) return default;
var icon = CreateIconWithSrc(srcIcon, size);
return icon;
}

public static Texture2D CreateIcon(string name, int size = 24)
{
var srcIcon = GetIcon(name);
if (srcIcon == null) return default;
var icon = CreateIconWithSrc(srcIcon, size);
return icon;
}

public static Texture2D GetIcon(Type type)
{
if (type != null) return EditorGUIUtility.ObjectContent(null, type).image as Texture2D;
return default;
}

public static Texture2D GetIcon(string name)
{
if (!string.IsNullOrEmpty(name))
{
var icon = EditorGUIUtility.FindTexture(name);
if (icon == null) icon = AssetDatabase.GetCachedIcon(name) as Texture2D;
if (icon == null) icon = EditorGUIUtility.IconContent(name).image as Texture2D;
return icon;
}

return default;
}

public static Texture2D CreateIconWithSrc(Texture2D srcIcon, int size = 24)
{
// Copy Built-in texture with RenderTexture
var tempRenderTexture = RenderTexture.GetTemporary(size, size, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
Graphics.Blit(srcIcon, tempRenderTexture);
var previousRenderTexture = RenderTexture.active;
RenderTexture.active = tempRenderTexture;
var icon = new Texture2D(size, size);
icon.ReadPixels(new Rect(0, 0, tempRenderTexture.width, tempRenderTexture.height), 0, 0);
icon.Apply();
RenderTexture.ReleaseTemporary(tempRenderTexture);
RenderTexture.active = previousRenderTexture;
return icon;
}

#endregion
}

}
#endif
11 changes: 11 additions & 0 deletions Editor/Script/GUI/EditorIcon.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
68 changes: 68 additions & 0 deletions Editor/Script/GUI/GUIMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Aya.DataBinding
{
public static partial class GUIMenu
{
public static void DrawSearchableDropdownMenu(string propertyName,
SearchableDropdown menu,
Func<bool> checkNullFunc,
Func<string> currentDisplayNameGetter)
{
var isNull = checkNullFunc();
using (GUIErrorColorArea.Create(isNull))
{
GUILayout.BeginHorizontal();
GUILayout.Label(propertyName, GUILayout.Width(EditorGUIUtility.labelWidth));

var displayName = currentDisplayNameGetter();
var currentPropertyDisplayName = isNull ? EditorStyle.NoneStr : displayName;
var btnRect = GUILayoutUtility.GetRect(new GUIContent(currentPropertyDisplayName), EditorStyles.toolbarButton);
var btnType = GUI.Button(btnRect, currentPropertyDisplayName, EditorStyles.popup);
if (btnType)
{
btnRect.width = EditorGUIUtility.currentViewWidth;
menu.Show(btnRect, 500f);
}

GUILayout.EndHorizontal();
}
}

public static SearchableDropdown CreateSearchableDropdownMenu<T>(string menuTitle,
IEnumerable<T> valueSource,
Func<T, string> pathGetter,
Func<T, Texture2D> iconGetter,
Action<T> onClick = null)
{
var root = new SearchableDropdownItem(menuTitle);
var menu = new SearchableDropdown(root, item =>
{
var value = (T)item.Value;
onClick?.Invoke(value);
});

root.AddChild(new SearchableDropdownItem(EditorStyle.NoneStr, null));
root.AddSeparator();

foreach (var value in valueSource)
{
var path = pathGetter(value);
var icon = iconGetter(value);
var item = new SearchableDropdownItem(path, value)
{
icon = icon
};

root.AddChild(item);
}

return menu;
}
}
}
#endif
11 changes: 11 additions & 0 deletions Editor/Script/GUI/GUIMenu.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Editor/Script/Util/GUIStruct.cs → Editor/Script/GUI/GUIStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,29 @@ public void Dispose()
GUI.color = OriginalColor;
}
}

public struct GUIErrorColorArea : IDisposable
{
public Color OriginalColor;

public static GUIErrorColorArea Create(bool check = true)
{
return new GUIErrorColorArea(check);
}

public GUIErrorColorArea(bool check = true)
{
OriginalColor = GUI.color;
if (check)
{
GUI.color = new Color(1f, 0.5f, 0.5f);
}
}

public void Dispose()
{
GUI.color = OriginalColor;
}
}
}
#endif
File renamed without changes.
Loading

0 comments on commit a61fc6d

Please sign in to comment.