2023-04-03 11:04:31 +08:00

136 lines
4.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using BF;
namespace BFEditor
{
[InitializeOnLoad]
public class HierarchyEditor
{
static EditorApplication.HierarchyWindowItemCallback hierarchyAction;
static Type[] systemComponents;
static Type[] customComponents;
static Type[] customAttributeComps;
static HierarchyEditor()
{
hierarchyAction = new EditorApplication.HierarchyWindowItemCallback(DrawCustomHierarchy);
EditorApplication.hierarchyWindowItemOnGUI += hierarchyAction;
systemComponents = new Type[] {
typeof(Camera),
typeof(Light),
typeof(AudioSource),
typeof(ParticleSystem),
typeof(Animator),
typeof(Animation),
typeof(MeshRenderer),
typeof(MeshFilter),
typeof(SkinnedMeshRenderer),
typeof(Rigidbody),
typeof(BoxCollider),
typeof(BoxCollider2D),
typeof(BoxCollider2D),
typeof(Image),
typeof(Text),
typeof(RawImage),
typeof(Button),
typeof(Toggle),
typeof(ToggleGroup),
typeof(Slider),
typeof(InputField),
typeof(Scrollbar),};
customComponents = new Type[] {
typeof(PrefabHelper),
};
customAttributeComps = new Type[] {
typeof(Image),
typeof(Text),
typeof(RawImage),
typeof(EmptyRaycast),
typeof(TMPro.TextMeshProUGUI),
};
}
static void DrawCustomHierarchy(int instanceId, Rect selectionRect)
{
GameObject go = (GameObject)EditorUtility.InstanceIDToObject(instanceId);
if (go == null)
return;
int index = 0;
DrawActiveToggle(go, selectionRect, ref index);
DrawSystemComponentIcon(go, selectionRect, ref index);
DrawCustomComponentIcon(go, selectionRect, ref index);
DrawComponentAttritube(go, selectionRect, ref index);
}
static void DrawActiveToggle(GameObject go, Rect selectionRect, ref int index)
{
var active1 = go.activeSelf;
go.SetActive(GUI.Toggle(GetRect(selectionRect, ref index), go.activeSelf, ""));
var active2 = go.activeSelf;
if (active1 != active2)
{
EditorUtility.SetDirty(go);
}
}
static void DrawSystemComponentIcon(GameObject go, Rect selectionRect, ref int index)
{
for (int i = 0; i < systemComponents.Length; i++)
{
if (go.GetComponent(systemComponents[i]) == null)
continue;
Texture texture = BFEditorUtils.GetSystemIcon(systemComponents[i]);
Rect rect = GetRect(selectionRect, ref index);
GUI.Label(rect, texture);
}
}
static void DrawCustomComponentIcon(GameObject go, Rect selectionRect, ref int index)
{
for (int i = 0; i < customComponents.Length; i++)
{
if (go.GetComponent(customComponents[i]) == null)
continue;
Texture texture = BFEditorUtils.GetCustomComponentIcon(customComponents[i]);
Rect rect = GetRect(selectionRect, ref index);
GUI.Label(rect, texture);
}
}
static void DrawComponentAttritube(GameObject go, Rect selectionRect, ref int index)
{
for (int i = 0; i < customAttributeComps.Length; i++)
{
if (go.GetComponent(customAttributeComps[i]) == null)
continue;
if (customAttributeComps[i] == typeof(Text) || customAttributeComps[i] == typeof(Image)
|| customAttributeComps[i] == typeof(RawImage) || customAttributeComps[i] == typeof(EmptyRaycast)
|| customAttributeComps[i] == typeof(TMPro.TextMeshProUGUI))
{
MaskableGraphic mg = go.GetComponent(customAttributeComps[i]) as MaskableGraphic;
if (mg.raycastTarget)
{
Rect rect = GetRect(selectionRect, ref index);
GUIStyle gs = new GUIStyle();
gs.normal.textColor = Color.red;
GUI.Label(rect, "RT", gs);
}
}
}
}
static Rect GetRect(Rect selectionRect, ref int index)
{
Rect tempRect = new Rect(selectionRect);
tempRect.x = selectionRect.x + selectionRect.width - index * 20;
index++;
return tempRect;
}
}
}