Set inspector icon for GameObjects via C# script in Unity

Set inspector icon for GameObjects via C# script in Unity
Photo by Alexander Shatov / Unsplash

Ever wondered how you can set the inspector icon for GameObjects via a C# script in Unity? Especially without reflection, which is the old solution to do.

GameObject inspector icon

Execute this in an editor script:

var iconContent = EditorGUIUtility.IconContent("sv_label_1");
EditorGUIUtility.SetIconForObject(gameObject, (Texture2D) iconContent.image);

sv_label_1 is the blue rounded rectangle. For other icons, see this nice list of all Unity built-in editor icons: https://github.com/halak/unity-editor-icons