Add: component icons

This commit is contained in:
AnnulusGames 2024-02-16 20:41:55 +09:00
parent 9d87b79807
commit 70e2f0e862
4 changed files with 85 additions and 57 deletions

View File

@ -33,8 +33,10 @@ namespace Alchemy.Editor
[SerializeField] HierarchyObjectMode hierarchyObjectMode = HierarchyObjectMode.RemoveInBuild;
[SerializeField] bool showHierarchyToggles;
[SerializeField] bool showComponentIcons;
public HierarchyObjectMode HierarchyObjectMode => hierarchyObjectMode;
public bool ShowHierarchyToggles => showHierarchyToggles;
public bool ShowComponentIcons => showComponentIcons;
}
}

View File

@ -1,73 +1,47 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace Alchemy.Editor
{
internal static class AlchemySettingsProvider
{
static readonly string MenuName = "Project/Alchemy";
static SerializedObject serializedObject;
[SettingsProvider]
public static SettingsProvider CreateSettingsProvider()
{
static Label CreateHeader(string text)
{
return new Label(text)
{
style = {
unityFontStyleAndWeight = FontStyle.Bold,
marginLeft = 2f,
marginTop = 3f,
}
};
}
return new SettingsProvider(MenuName, SettingsScope.Project)
{
label = "Alchemy",
keywords = new HashSet<string>(new[] { "Alchemy, Inspector, Hierarchy" }),
activateHandler = (searchContext, rootElement) =>
guiHandler = searchContext =>
{
var serializedObject = new SerializedObject(AlchemySettings.GetOrCreateSettings());
if (serializedObject == null) serializedObject = new(AlchemySettings.GetOrCreateSettings());
else serializedObject.Update();
var root = new VisualElement
using (new EditorGUILayout.HorizontalScope())
{
style = {
marginLeft = 10f
GUILayout.Space(10f);
using (new EditorGUILayout.VerticalScope())
{
EditorGUILayout.LabelField("Hierarchy", EditorStyles.boldLabel);
using (var changeCheck = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("hierarchyObjectMode"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("showHierarchyToggles"), new GUIContent("Show Toggles"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("showComponentIcons"));
if (changeCheck.changed)
{
serializedObject.ApplyModifiedProperties();
AlchemySettings.SaveSettings();
}
}
}
};
rootElement.Add(root);
var label = new Label("Alchemy")
{
style = {
unityFontStyleAndWeight = FontStyle.Bold,
fontSize = 20,
marginTop = 3f,
marginBottom = 5f
}
};
root.Add(label);
var hierarchyHeader = CreateHeader("Hierarchy");
root.Add(hierarchyHeader);
var hierarchyObjectModeField = new PropertyField(serializedObject.FindProperty("hierarchyObjectMode"));
root.Add(hierarchyObjectModeField);
var showHierarchyTogglesField = new PropertyField(serializedObject.FindProperty("showHierarchyToggles"))
{
label = "Show Toggles"
};
root.Add(showHierarchyTogglesField);
root.Bind(serializedObject);
root.TrackSerializedObjectValue(serializedObject, so =>
{
AlchemySettings.SaveSettings();
});
}
},
};
}

View File

@ -1,24 +1,29 @@
using UnityEditor;
using UnityEngine;
using Alchemy.Hierarchy;
using System.Linq;
namespace Alchemy.Editor
{
public sealed class HierarchyButtonDrawer : HierarchyDrawer
{
static readonly GUIContent ScriptIcon = EditorGUIUtility.IconContent("cs Script Icon");
public override void OnGUI(int instanceID, Rect selectionRect)
{
var gameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (gameObject == null) return;
if (gameObject.TryGetComponent<HierarchyObject>(out _)) return;
if (AlchemySettings.GetOrCreateSettings().ShowHierarchyToggles)
{
var pos = selectionRect;
pos.x = pos.xMax - 8f;
pos.width = 16f;
var settings = AlchemySettings.GetOrCreateSettings();
var active = GUI.Toggle(pos, gameObject.activeSelf, string.Empty);
if (settings.ShowHierarchyToggles)
{
var rect = selectionRect;
rect.x = rect.xMax - 5f;
rect.width = 16f;
var active = GUI.Toggle(rect, gameObject.activeSelf, string.Empty);
if (active != gameObject.activeSelf)
{
Undo.RecordObject(gameObject, $"{(active ? "Activate" : "Deactivate")} GameObject '{gameObject.name}'");
@ -26,6 +31,52 @@ namespace Alchemy.Editor
EditorUtility.SetDirty(gameObject);
}
}
if (settings.ShowComponentIcons)
{
var rect = selectionRect;
rect.x = rect.xMax - (settings.ShowHierarchyToggles ? 22f : 5f);
rect.y += 1f;
rect.width = 14f;
rect.height = 14f;
var components = gameObject
.GetComponents<Component>()
.AsEnumerable()
.Reverse();
var existsScriptIcon = false;
foreach (var component in components)
{
var image = AssetPreview.GetMiniThumbnail(component);
if (image == null) continue;
if (image == ScriptIcon.image)
{
if (existsScriptIcon) continue;
existsScriptIcon = true;
}
DrawIcon(ref rect, image, IsEnabled(component) ? Color.white : new(1f, 1f, 1f, 0.5f));
}
}
}
static void DrawIcon(ref Rect rect, Texture image, Color color)
{
var defaultColor = color;
GUI.color = color;
GUI.DrawTexture(rect, image, ScaleMode.ScaleToFit);
rect.x -= rect.width;
GUI.color = defaultColor;
}
static bool IsEnabled(Component component)
{
var property = component.GetType().GetProperty("enabled", typeof(bool));
return (bool)(property?.GetValue(component, null) ?? true);
}
}

View File

@ -1,4 +1,5 @@
{
"hierarchyObjectMode": 2,
"showHierarchyToggles": true
"showHierarchyToggles": true,
"showComponentIcons": true
}