some wise words
Want to be notified of new blog posts?


Elwin Verploegen

Snippet: Automatically Add Scene LabelsDecember 19, 2014

By Elwin

This script was written by Thundernerd on the Unity3D Answers Forum at the following link: http://answers.unity3d.com/questions/542890/scene-color-object-marking.html.

The following script allows you to automatically add a Label on a GameObject in a scene, here’s a screenshot on how that looks in the scene:

Scene Label

Scene Label

 using System;
 using System.Reflection;
 using UnityEditor;
 using UnityEngine;
 
 public class IconManager {
 
     public enum LabelIcon {
         Gray = 0,
         Blue,
         Teal,
         Green,
         Yellow,
         Orange,
         Red,
         Purple
     }
 
     public enum Icon {
         CircleGray = 0,
         CircleBlue,
         CircleTeal,
         CircleGreen,
         CircleYellow,
         CircleOrange,
         CircleRed,
         CirclePurple,
         DiamondGray,
         DiamondBlue,
         DiamondTeal,
         DiamondGreen,
         DiamondYellow,
         DiamondOrange,
         DiamondRed,
         DiamondPurple
     }
 
     private static GUIContent[] labelIcons;
     private static GUIContent[] largeIcons;
 
     public static void SetIcon( GameObject gObj, LabelIcon icon ) {
         if ( labelIcons == null ) {
             labelIcons = GetTextures( "sv_label_", string.Empty, 0, 8 );
         }
 
         SetIcon( gObj, labelIcons[(int)icon].image as Texture2D );
     }
 
     public static void SetIcon( GameObject gObj, Icon icon ) {
         if ( largeIcons == null ) {
             largeIcons = GetTextures( "sv_icon_dot", "_pix16_gizmo", 0, 16 );
         }
 
         SetIcon( gObj, largeIcons[(int)icon].image as Texture2D );
     }
 
     private static void SetIcon( GameObject gObj, Texture2D texture ) {
         var ty = typeof( EditorGUIUtility );
         var mi = ty.GetMethod( "SetIconForObject", BindingFlags.NonPublic | BindingFlags.Static );
         mi.Invoke( null, new object[] { gObj, texture } );
     }
 
     private static GUIContent[] GetTextures( string _b, string postFix, int si, int c ) {
         GUIContent[] guiContentArray = new GUIContent[c];
 
         var t = typeof( EditorGUIUtility );
         var mi = t.GetMethod( 
                    "IconContent", 
                    BindingFlags.Public | BindingFlags.Static, 
                    null, 
                    new Type[] { typeof( string ) }, 
                    null 
                );
 
         for ( int index = 0; index < c; ++index ) {
             guiContentArray[index] = mi.Invoke( 
                                        null, 
                                        new object[] { 
                                                    _b + (object)(si + index) + postFix 
                                                }
                                    ) as GUIContent;
         }
 
         return guiContentArray;
     }
 }

Create a new file in the Editor directory named IconManager and copy the above code into it. If you want to set the label of a GameObject you can call the code below.

IconManager.SetIcon(obj, IconManager.LabelIcon.Blue);

Where obj is the GameObject that you want to give a label.

Want to be notified of new blog posts?