Programmatically assign a Label Icon to a Game Object

Hey all -

I couldn’t find this in the docs, is there a way to programmatically assign an icon to a Game Object you dynamically create via Editor script?

[Edit] To clarify: I am aware of Gizmos.DrawIcon - what I am looking for is to programmatically assign a Label Icon to a Game Object that I am dynamically creating.

Thanks!

==

Continuing on @ArkaneX answer linked above. In my case, I didn’t want a custom icon loaded via resources or whatever, I just wanted to programmatically access and set the gameObject icons that we see in the popup when we click the top section. It’s easy, as long as you know where to look and how to find it :slight_smile:

private void DrawIcon(GameObject gameObject, int idx)
{
	var largeIcons = GetTextures("sv_label_", string.Empty, 0, 8);
	var icon = largeIcons[idx];
	var egu = typeof(EditorGUIUtility);
	var flags = BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic;
	var args = new object[] { gameObject, icon.image };
	var setIcon = egu.GetMethod("SetIconForObject", flags, null, new Type[]{typeof(UnityEngine.Object), typeof(Texture2D)}, null);
	setIcon.Invoke(null, args);
}

private GUIContent[] GetTextures(string baseName, string postFix, int startIndex, int count)
{
	GUIContent[] array = new GUIContent[count];
	for (int i = 0; i < count; i++)
	{
		array *= EditorGUIUtility.IconContent(baseName + (startIndex + i) + postFix);*
  •   }*
    
  •   return array;*
    
  • }*
    The previous example draws the ‘large’ label icons (there’s 8 of them). Possible string values:
    “sv_icon_name” with a “” postFix (label icons: max 8),
    “sv_icon_dot” with a “_sml” postFix (small icon, max 16),
    “sv_icon_dot” with a “_pix16_gizmo” postFix (large icon, max 16)

Very old question, but please take a look here: Scene color object marking - Questions & Answers - Unity Discussions