Last active
November 16, 2021 21:58
-
-
Save shinriyo/542d8cef6fc7c579470f to your computer and use it in GitHub Desktop.
This file contains 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 System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
[InitializeOnLoad] | |
/// <summary> | |
/// Draw component icon in hierarchy. | |
/// </summary> | |
public class DrawComponentIconInHierarchy | |
{ | |
const int iconSize = 16; | |
/// <summary> | |
/// Initializes the <see cref="DrawComponentIconInHierarchy"/> class. | |
/// </summary> | |
static DrawComponentIconInHierarchy() | |
{ | |
EditorApplication.hierarchyWindowItemOnGUI += (instanceID, rect) => | |
{ | |
DrawComponentIcons(instanceID, rect); | |
}; | |
} | |
/// <summary> | |
/// Draws the component icons. | |
/// </summary> | |
/// <param name="instanceID">Instance I.</param> | |
/// <param name="rect">Rect.</param> | |
static void DrawComponentIcons(int instanceID, Rect rect) | |
{ | |
rect.x += rect.width; | |
rect.width = iconSize; | |
HashSet<Component> components = GetComponents(instanceID); | |
if(components == null) | |
{ | |
return; | |
} | |
HashSet<Texture> textures = new HashSet<Texture>(); | |
foreach(Component component in components) | |
{ | |
if(component != null) | |
{ | |
textures.Add(AssetPreview.GetMiniThumbnail(component)); | |
} | |
} | |
foreach(var texture in textures) | |
{ | |
rect.x -= iconSize; | |
GUI.DrawTexture(rect, texture); | |
} | |
} | |
/// <summary> | |
/// Gets the components. | |
/// </summary> | |
/// <returns>The components.</returns> | |
/// <param name="instanceID">Instance I.</param> | |
static HashSet<Component> GetComponents(int instanceID) | |
{ | |
HashSet <Component> components = new HashSet<Component>(); | |
GameObject sceneGameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject; | |
if(sceneGameObject == null) | |
{ | |
return null; | |
} | |
var sceneGameObjectComponents = sceneGameObject.GetComponents<Component>(); | |
if(sceneGameObjectComponents == null) | |
{ | |
return null; | |
} | |
foreach(Component component in sceneGameObjectComponents) | |
{ | |
if(component is UnityEngine.Transform) | |
{ | |
continue; | |
} | |
components.Add(component); | |
} | |
return components; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment