Skip to content

Instantly share code, notes, and snippets.

@ilkinulas
Created July 20, 2016 18:40
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ilkinulas/802c993bb6bcdb3a45bfbdd01c2f3718 to your computer and use it in GitHub Desktop.
Save ilkinulas/802c993bb6bcdb3a45bfbdd01c2f3718 to your computer and use it in GitHub Desktop.
editor script that demonstrates how to customize hierarchy window
using UnityEngine;
using UnityEditor;
using System.Text;
[InitializeOnLoad]
public class CustomHierarchyView {
private static StringBuilder sb = new StringBuilder ();
static CustomHierarchyView() {
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
}
static void HierarchyWindowItemOnGUI (int instanceID, Rect selectionRect) {
GameObject gameObject = EditorUtility.InstanceIDToObject (instanceID) as GameObject;
DisplayActiveAndPassiveCardCount (selectionRect, gameObject);
}
static void DisplayActiveAndPassiveCardCount (Rect selectionRect, GameObject gameObject)
{
Card[] cards = gameObject.GetComponentsInChildren<Card> (includeInactive: true);
int total = cards.Length;
if (total > 0 && gameObject.GetComponent<Card> () == null) {
ActivePassive pair = ProcessCards (cards);
Rect r = new Rect (selectionRect);
r.x += r.width - 65;
sb.Length = 0;
sb.Append (total).Append (" (").Append (pair.active).Append (", ").Append (pair.passive).Append (")");
GUI.Label (r, sb.ToString ());
}
}
private static ActivePassive ProcessCards(Card [] cards) {
ActivePassive pair = new ActivePassive ();
for (int i = 0; i < cards.Length; i++) {
Card card = cards [i];
if (card.gameObject.activeInHierarchy) {
pair.active++;
} else {
pair.passive++;
}
}
return pair;
}
}
struct ActivePassive {
public int active;
public int passive;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment