Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by equalsequals · Feb 02, 2012 at 10:56 PM · editoreditorgui

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!

==

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Feb 03, 2012 at 12:24 AM 1
Share

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 ?

avatar image equalsequals · Feb 03, 2012 at 12:25 AM 1
Share

In my edit I added a link which takes you to the docs about Label Icons.

avatar image Berenger · Feb 03, 2012 at 12:30 AM 0
Share

Waoh, can't believe I never saw that, thank you :)

2 Replies

· Add your reply
  • Sort: 
avatar image
13

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)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Smireles · Apr 16, 2020 at 03:15 PM 0
Share

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 !

avatar image Tortuap · May 07, 2021 at 08:48 AM 0
Share

This code is awful. What a terrible code. @ArkaneX answer is so much clearer, cleaner, reusable. It should be the answer to that question.

avatar image syllabusgames · May 10, 2021 at 04:37 PM 0
Share

You will need to include: using UnityEditor; using System; using System.Reflection;

avatar image
5

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aram-Azhari · Jun 29, 2014 at 07:02 AM 0
Share

This does not work anymore as SetIconForObject has been removed from EditorGUIUtility

avatar image vexe · Jan 14, 2015 at 01:16 AM 1
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges