Search Unity

The icons next to window tabs for Unity's built-in windows: Scene, Game, Project...

Discussion in 'Editor & General Support' started by tuncturel, May 12, 2013.

  1. tuncturel

    tuncturel

    Joined:
    Jul 20, 2012
    Posts:
    47
    I was looking around on how to make my own windows in Unity. I started looking at Extending the Editor page which helped me quite a lot, even though it was missing C# examples for the code. I was able to make my own window and label it under the 'Window' tab, however I couldn't find a way to place icons like the ones Unity has with its own windows. For example there's a Pacman logo next to the Game window's tab.

    $Pacman Icon next to Game window.png

    I figured you could do it with text since you declare the name of the window by the following:

    Code (csharp):
    1. [MenuItem("Window/NameOfMyWindow")]
    I tried some ASCII characters, but Unity doesn't display them.

    Can someone help me with this? I want to either put a special Icon I will make there or I want to have a list of characters that I can use so I can make my window look super cool. : )
     
  2. Flipbookee

    Flipbookee

    Joined:
    Jun 2, 2012
    Posts:
    2,796
    Unfortunately there's no way to set the window icon using Unity's API... Fortunately there is a way to do that using Reflection! ;) But that's kind of advanced a little bit... And you shouldn't worry about missing the icon too much. Well, unless you really want to make your window look super cool... Just keep in mind that commercial plugins do not put an icon in their windows, at least no one that I know about is doing that. Except the Favorites Tab (https://www.assetstore.unity3d.com/#/content/4237) which is a masterpiece on it's own anyway, and that icon there is just icing on the cake. :cool:

    This is how it's done in the Favorites Tab:

    Code (csharp):
    1. class SuperCoolWindow : EditorWindow
    2. {
    3. // ...
    4.     static Texture2D windowIcon;
    5.  
    6.  
    7.     PropertyInfo cachedTitleContent;
    8.  
    9.     void OnEnable()    {
    10.         // Load your icon texture here and store it in windowIcon...
    11.  
    12.  
    13.         if (cachedTitleContent == null)
    14.         {
    15.             cachedTitleContent = typeof(EditorWindow).GetProperty("cachedTitleContent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField);
    16.         }
    17.         if (cachedTitleContent != null)
    18.         {
    19.             GUIContent titleContent = cachedTitleContent.GetValue(this, null) as GUIContent;
    20.             if (titleContent != null)
    21.             {
    22.                 titleContent.image = windowIcon;
    23.                 titleContent.text = "Super Cool"; // <= here goes the title of your window
    24.             }
    25.         }
    26.     }
    27.  
    28. // ...
    29.  
    30.     public void OnGUI()
    31.     {
    32.         if (cachedTitleContent != null)
    33.         {
    34.             GUIContent titleContent = cachedTitleContent.GetValue(this, null) as GUIContent;
    35.             if (titleContent != null)
    36.                 titleContent.image = windowIcon;
    37.         }
    38.  
    39.         // ...
    40.     }
    41.  
    42. }
    43.  
    And you'll get something similar to this:
    $FavsTabsSingle.png

    Isn't it super cool? :cool:
     
  3. tuncturel

    tuncturel

    Joined:
    Jul 20, 2012
    Posts:
    47
    This was perfect! Thank you very much. I immediately implemented an icon using this code. It's a little tricky to get the positioning right but hey!

    It's super cool :cool: !
     
  4. Flipbookee

    Flipbookee

    Joined:
    Jun 2, 2012
    Posts:
    2,796
    Screenshots to prove the coolness? ;)
     
  5. Bradock

    Bradock

    Joined:
    Mar 6, 2013
    Posts:
    14
    In Unity 4 you can place your window icon in "Assets/Editor Default Resources/Icons/". The image can be a png or tiff.
    Your window should have the same name as the icon.
    Image and code bellow :)

    Code (csharp):
    1. EditorWindow.GetWindow<BehaviourWindow>("Behaviour");
    $Screen Shot 2013-09-15 at 12.50.13 AM cópia.png
     
  6. Flipbookee

    Flipbookee

    Joined:
    Jun 2, 2012
    Posts:
    2,796
    Ah, that's much nicer now! A bit limited though... it would be even better if UT exposed an 'icon' property for example, because my Favourites Tab changes its title to show the selected filtering mode, and soon it will have to change icon to show filtering by star color. If it was exposed as a property some windows could change or even animated the icon to indicate busy state or warnings and errors... Still extensions targeting earlier versions of Unity have no other choice than using my hack ;)