Created
January 31, 2018 14:47
-
-
Save aberloni/3ee25c967ecd2cec0c3ce84bd31d3f64 to your computer and use it in GitHub Desktop.
Draw icon next to object that have a specific component. Must be matching a [editor data]/icons/icon_[Type].png as first child (after Transform).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.Collections.Generic; | |
[InitializeOnLoad] | |
public class EditorTypeIconDrawer | |
{ | |
private static Dictionary<Type,Texture> icons; | |
private static bool isRegistered = false; | |
static EditorTypeIconDrawer() | |
{ | |
RegisterGUI(true); | |
if (icons == null) icons = new Dictionary<Type, Texture>(); | |
foreach(KeyValuePair<Type, Texture> icon in icons) | |
{ | |
Debug.Log(icon.Key); | |
} | |
} | |
static private void RegisterGUI(bool register) | |
{ | |
if (register) | |
{ | |
if (!isRegistered) | |
{ | |
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI; | |
EditorApplication.update += Update; | |
isRegistered = true; | |
} | |
} | |
else | |
{ | |
EditorApplication.hierarchyWindowItemOnGUI -= HierarchyWindowItemOnGUI; | |
EditorApplication.update -= Update; | |
isRegistered = false; | |
} | |
} | |
static void Update() | |
{ | |
//foreach (KeyValuePair<Type, Texture> icon in icons) if(icon.Value != null) Debug.Log(icon.Key + " ? " + icon.Value); | |
EditorApplication.RepaintHierarchyWindow(); | |
} | |
static private Texture refreshIcon(Type componentType) | |
{ | |
if (icons.ContainsKey(componentType) && icons[componentType] != null) return icons[componentType]; | |
Texture icon = (Texture)EditorGUIUtility.Load("icons/icon_" + componentType.ToString() + ".png"); | |
if (icons.ContainsKey(componentType)) icons[componentType] = icon; | |
else icons.Add(componentType, icon); | |
return icon; | |
} | |
static private void HierarchyWindowItemOnGUI(int selectionID, Rect selectionRect) | |
{ | |
GameObject go = (GameObject)EditorUtility.InstanceIDToObject(selectionID); | |
if (go == null) return; | |
Component[] comps = go.GetComponents<Component>(); | |
if (comps.Length < 2) return; | |
Component firstComp = go.GetComponents<Component>()[1]; | |
//Debug.Log(firstComp.GetType()); | |
if (firstComp == null) return; | |
//generate icon | |
Texture icon = refreshIcon(firstComp.GetType()); | |
//no icon for that type | |
if (icon == null) return; | |
selectionRect.xMin -= (go.transform.childCount == 0) ? 15f : 29f; | |
selectionRect.yMin += 1f; | |
selectionRect.width = 14f; | |
selectionRect.height = 14f; | |
GUI.DrawTexture(selectionRect, icon); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment