- Home /
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!
==
As my answer won't get you anywhere but take your question out of the unanswered ones, I deleted it. Here is the conversation though.
$$anonymous$$y answer : I'm not sure what you mean, but I guess it's something like Gizmos.DrawIcon.
equalsequals : As of 3.4 you can assign an icon to a Game Object via the inspector - I want to know if I can programmatically access that when I create a new Game Object from an Editor script. Gizmos.DrawIcon requires me to manually write the icon draw logic in a component's script - which I'd rather not do if the Editor can assign one for you.
$$anonymous$$e again : That's interesting, how can you do that from the inspector ?
In my edit I added a link which takes you to the docs about Label Icons.
Answer by vexe · Jan 14, 2015 at 01:23 AM
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 :)
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[i] = 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)
I just added those two functions as the barbarian that I am (adding the needed dependencies first) and it worked like a charm. $$anonymous$$any thanks @vexe !
This code is awful. What a terrible code. @ArkaneX answer is so much clearer, cleaner, reusable. It should be the answer to that question.
You will need to include: using UnityEditor; using System; using System.Reflection;
Answer by ArkaneX · Sep 25, 2013 at 10:48 PM
Very old question, but please take a look here: http://answers.unity3d.com/questions/542890/scene-color-object-marking.html
This does not work anymore as SetIconForObject has been removed from EditorGUIUtility
@Aram Azhari, I don't know what you're talking about. SetIconForObject
is 'not' removed for EditorGUIUtility, it's just internal, not public.
Your answer
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
Assigning a script in custom editor 1 Answer
What are these blue icons over assets in the unity editor? 2 Answers
How to properly handle Undo events in custom inspector? 0 Answers
How can i create a pup-up menu like the one for choose the shader on materials in unity 2019? 1 Answer