Skip to content

Instantly share code, notes, and snippets.

@Volantk
Last active June 20, 2024 13:31
Show Gist options
  • Save Volantk/70198e90633f8f22d95dacd176266f4a to your computer and use it in GitHub Desktop.
Save Volantk/70198e90633f8f22d95dacd176266f4a to your computer and use it in GitHub Desktop.
Set GameObject icon in Unity
public static void SetGameObjectIcon(GameObject go, string texturePath)
{
Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath);
if (texture == null)
{
Debug.LogError("Couldn't find an icon...");
return;
}
var so = new SerializedObject(go);
var iconProperty = so.FindProperty("m_Icon");
iconProperty.objectReferenceValue = texture;
so.ApplyModifiedProperties();
}
public static void SetGameObjectIcon(GameObject go, EditorIconType iconType)
{
Texture2D texture = EditorGUIUtility.FindTexture(GetIconPath(iconType));
if (texture == null)
{
Debug.LogError("Couldn't find an icon...");
return;
}
var so = new SerializedObject(go);
var iconProperty = so.FindProperty("m_Icon");
iconProperty.objectReferenceValue = texture;
so.ApplyModifiedProperties();
}
public enum EditorIconType
{
GreyLabel,
BlueLabel,
CyanLabel,
GreenLabel,
YellowLabel,
OrangeLabel,
RedLabel,
PinkLabel,
GreyRound,
BlueRound,
CyanRound,
GreenRound,
YellowRound,
OrangeRound,
RedRound,
PinkRound,
GreyDiamond,
BlueDiamond,
CyanDiamond,
GreenDiamond,
YellowDiamond,
OrangeDiamond,
RedDiamond,
PinkDiamond
}
private static string GetIconPath(EditorIconType iconType)
{
switch (iconType)
{
case EditorIconType.GreyLabel: return "sv_label_0";
case EditorIconType.BlueLabel: return "sv_label_1";
case EditorIconType.CyanLabel: return "sv_label_2";
case EditorIconType.GreenLabel: return "sv_label_3";
case EditorIconType.YellowLabel: return "sv_label_4";
case EditorIconType.OrangeLabel: return "sv_label_5";
case EditorIconType.RedLabel: return "sv_label_6";
case EditorIconType.PinkLabel: return "sv_label_7";
case EditorIconType.GreyRound: return "sv_icon_dot0_pix16_gizmo";
case EditorIconType.BlueRound: return "sv_icon_dot1_pix16_gizmo";
case EditorIconType.CyanRound: return "sv_icon_dot2_pix16_gizmo";
case EditorIconType.GreenRound: return "sv_icon_dot3_pix16_gizmo";
case EditorIconType.YellowRound: return "sv_icon_dot4_pix16_gizmo";
case EditorIconType.OrangeRound: return "sv_icon_dot5_pix16_gizmo";
case EditorIconType.RedRound: return "sv_icon_dot6_pix16_gizmo";
case EditorIconType.PinkRound: return "sv_icon_dot7_pix16_gizmo";
case EditorIconType.GreyDiamond: return "sv_icon_dot8_pix16_gizmo";
case EditorIconType.BlueDiamond: return "sv_icon_dot9_pix16_gizmo";
case EditorIconType.CyanDiamond: return "sv_icon_dot10_pix16_gizmo";
case EditorIconType.GreenDiamond: return "sv_icon_dot11_pix16_gizmo";
case EditorIconType.YellowDiamond: return "sv_icon_dot12_pix16_gizmo";
case EditorIconType.OrangeDiamond: return "sv_icon_dot13_pix16_gizmo";
case EditorIconType.RedDiamond: return "sv_icon_dot14_pix16_gizmo";
case EditorIconType.PinkDiamond: return "sv_icon_dot15_pix16_gizmo";
default:
throw new ArgumentOutOfRangeException("iconType", iconType, null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment