Is it possible to change color that is next to tag in inspector menu via script? And if yes, how would it look like in C#?
(I mean the color that objects are refered with mark or name (not the color of objects) in Scene editor and which is not seenable while running Game window)
EDIT: Please go here for the latest version GitHub - Thundernerd/Unity3D-IconManager: A small script allowing you to set icons for GameObjects through code
The latest version has the following features:
- Option to remove icons
- Option to use a custom icon
- Can be included into final builds but the code won’t execute anything
For anyone who stumbles on this, here you go.
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 baseName, string postFix, int startIndex, int count ) {
GUIContent[] guiContentArray = new GUIContent[count];
var t = typeof( EditorGUIUtility );
var mi = t.GetMethod( "IconContent", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof( string ) }, null );
for ( int index = 0; index < count; ++index ) {
guiContentArray[index] = mi.Invoke( null, new object[] { baseName + (object)(startIndex + index) + postFix } ) as GUIContent;
}
return guiContentArray;
}
}
Yes - through reflection. Example:
using UnityEngine;
using UnityEditor;
using System.Reflection;
public class CreateObject : Editor
{
[MenuItem("Tools/Create object with icon")]
static void CreateSphereWithIcon()
{
Texture2D icon = (Texture2D)Resources.Load("CustomIcon");
var go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
var editorGUIUtilityType = typeof(EditorGUIUtility);
var bindingFlags = BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic;
var args = new object[] { go, icon };
editorGUIUtilityType.InvokeMember("SetIconForObject", bindingFlags, null, null, args);
//var editorUtilityType = typeof(EditorUtility);
//editorUtilityType.InvokeMember("ForceReloadInspectors", bindingFlags, null, null, null);
}
}
ForceReloadInspector method is called in original source (inside UnityEditor.IconSelector.DoButton method), but when I tested above solution, the call was not required. I left these lines here ‘just in case’, so if you see any problems, try uncommenting them.