Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to add the icon in EditorWindow tab ?

Discussion in 'Editor & General Support' started by Neodrop, Aug 24, 2009.

  1. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    Please, help me. How can I add the icon in the EditorWIndow tab?
     

    Attached Files:

  2. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    And one more : can I add my EditorWindow right in tabs like AssetServer or Inspector Window?
     

    Attached Files:

  3. cosmicmesh

    cosmicmesh

    Joined:
    Mar 19, 2009
    Posts:
    61
    I'd like to know about the icon thing, also... Can any of the UT guys chime in here, if you have a second?
     
  4. matthewminer

    matthewminer

    Joined:
    Aug 29, 2005
    Posts:
    331
    I asked this same question and the answer is that you can't customize the icon.
     
  5. cosmicmesh

    cosmicmesh

    Joined:
    Mar 19, 2009
    Posts:
    61
    Well, that's no fun. :(
     
  6. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    It should be pretty easy for them to expose the icon if enough people want it, so be sure to wish-list it if you haven't already.

    -Jeremy
     
  7. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    It would also be nice if the ICanHazCustomMenu interface was exposed so that our editor windows can implement it and have custom right-click menus. This is the part that goes above the "Add Tab" popout item (like Debug / Normal items in inspector).
     
  8. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    For anyone still interested, I found a way, using Reflection and accessing a hidden getter in the EditorWindow class.

    I added it as a simple method in my HOUnityLibs open-source library, inside the HOEditorUtils.HOPanelUtils static class. You can simply call:
    Code (csharp):
    1.  
    2. HOPanelUtils.SetWindowTitle(EditorWindow editorWindow, Texture icon, string title);
    3.  
    If you're curious about the code to do it, it's here.
     
    codestage likes this.
  9. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I found that if you give your EditorWindow the title "UnityEditor.DebugInspectorWindow" it gets an icon automatically. I am trying to figure out how this happens.
     
  10. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    It's just a matter of accessing the cachedTitleContent property in EditorWindow (though it has to be accessed via Reflection since it's private) and changing it. You can add icons because it's a GUIContent. Probably the UnityEditor.DebugInspectorWindow title corresponds to some already present icon.
     
  11. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    The image of the cached title is reset each time using "LoadGeneratedIconOrNormalIcon".
     
  12. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Interesting. Where is that method? Can't see it when inspecting EditorWindow
     
  13. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    The icon in the inspector can be loaded using:

    Texture2D t = EditorGUIUtility.Load("Icons/UnityEditor.DebugInspectorWindow.png") as Texture2D;

    If we can place a custom image in the "Icons" location then it will get picked automatically.

    Sorry missed your post, it is deep within UnityEditorInternal namespace
     
  14. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I've worked it out:

    Create the folder:

    Assets/Editor Default Resources/Icons/Your.Icon.png

    There must be a dot in the name of your icon for this to work. Then use the title for your window "Your.Icon"

    Also your image must not be too large otherwise it doesn't seem to get loaded.
     
  15. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    This looks very cool, thanks for sharing :) But! Can the Editor Default Resources folder be placed somewhere else or it can only be in the Assets folder? Otherwise you can't keep all you editorWindow assets in one place, which is a thing required by the Asset Store.
     
  16. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    It has to be in that exact folder. The default built-in ones are somehow placed into that path virtually. So there might be a way to load a resource with that asset path virtually also, but I am not sure how that works.

    PlayMaker actually does include the paths:

    Assets/Gizmos/...
    Assets/PlayMaker/...
    Assets/iTween/...

    So it must be possible to do the same thing for this, though it is undocumented and I am uncertain as to whether this would be approved by the asset store team. If there was a way to integrate this into your DLL then this would probably be okay. I would contact the asset store team, they usually get back quite quickly.
     
  17. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Great, thanks again :)
     
  18. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Okay, the editor resources are loaded using: EditorGUIUtility.GetEditorAssetBundle().Load(filename, type);

    So there may lie a clue for someone who understands how asset bundles work.
     
  19. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    And the answer to the "Add Tab" menu is that it is a fixed array of panel types. I have asked one of the Unity developers about adding items to the context menu (which requires the currently internal interface ICanHazCustomMenu) and they have agreed to make this fellow public. Unfortunately it doesn't seem that this will be available for Unity 3.x users and I was told that it probably wouldn't see the light of day until 4.1
     
  20. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Update on this. I've been testing in Unity 4 and the following seems to work fine for me.

    Create the /Assets/Editor Default Resources/Icons/YourWindowName.png file.
    No need for the dot in the name as stated before (the Your.Icon.png thing)
    It can be 16x16 and include transpaent areas, PNG is good for this.
    If you don't see it then 1st restart Unity before assuming it is not working.

    I've tried moving the Icons and DefRes folders to my package folder root, the Editor path, etc, nothing else worked than the exact path above. So the problem now is that the Asset Store won't pick it up as a dependancy when you want to submit a package. Any other ideas than the reflection thing? I would like to do it in a "normal" Unity way.

    [edit] Just thought of something. I could make a material in my package's folder and use that texture and thus make it a dependancy. I just want it included in the final pcakge uploaded to the AS and as far as I know these "dependancy" folders are also uploaded. A bit hack-ish though. I wonder if there is a cleaner way.
     
    Last edited: Jan 22, 2013
  21. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    PlayMaker is somehow including assets outside of its folder for gizmo icons and such, so it might be worth your while checking how they've done that.

    I would personally create an asset with the following class attached and then drag all of the assets that I wanted to include into the inspector field:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class ListOfDependencyAssets : ScriptableObject {
    4.     public Object[] dependencies;
    5. }
     
  22. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Thanks, I'll check that idea out. Was just starting to play around with this.
     
  23. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Another slightly less fiddly way to do this would be to use a prefab and a MonoBehaviour instead.
     
  24. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    I've made a prefab with a script/component on it, similar to script you posted above. I like it. Looks very clean with just the one script and the prefab.

    I wanna get away from using DLLs this time around (only used it cause of custom icons) and maybe update UniRPG Tile Ed later to also not need the DLLs. They become too much trouble to maintain, especially now with the differences between Unity 3.5 and 4.
     
    Last edited: Jan 22, 2013
  25. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    What issues have you had with compatibility between both versions and DLLs?

    I haven't experienced any issues whatsoever myself. I create my unity packages using Unity 3.5 and they work fine in both versions. Obviously some of the functionality is not available in Unity 3.5 due to the new features that have been added to Unity 4.0.
     
  26. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    The SetActive warnings, but the main one is EditorUtility.GetAssetPreview which is now AssetPreview.GetAssetPreview in Unity 4.
     
  27. FWCorey

    FWCorey

    Joined:
    Feb 2, 2011
    Posts:
    42
    I know this is an old thread, but it's interesting and...

    Add Tab can be pseudo expanded when ICanHazCustomMenu is implemented, but only for your newly added windows and not for the default editor windows.

    Just use:
    public void AddItemsToMenu(GenericMenu menu) {

    menu.AddItem(new GUIContent("Add Tab/Your First Tab"), false, FunctionToLoadYourFirstWindow);
    menu.AddItem(new GUIContent("Add Tab/Your Second Tab"), false, FunctionToLoadYourSecondWindow);
    }


    etc...
     
    Demigiant likes this.
  28. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    I know it's an old thread, but I'm going this way;

    Code (CSharp):
    1. titleInfo = base.GetType().GetProperty("cachedTitleContent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField);
    2. GUIContent title = titleInfo.GetValue(this, null) as GUIContent;
    3. title.image = HierarchyIcon;
    From the OnEnable method of my EditorWindow.
    I'm doing it this way because my icons are within a DLL, and not part of the asset folders.
     
  29. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Let's keep this one alive into 2015 ;)

    For my new project I am back at using DLLs but before I went with the reflection route I though of trying the "Editor Default Resources" and it works. I will probably do the reflection method and include the icon in the DLL, since that looks cleaner in the project, but here is how to get it to work in a "normal" manner...

    - This is tested in Unity 4.6
    - This method does work with EditorWindow that is in a DLL
    - I've tested with 16x16 PNG images.
    - You need to restart Unity after adding the icons (if the window class allready exist)
    - You must safe the icons in the following path in your project: "Assets/Editor Default Resources/Icons/"
    - You can specify which icon to load depending on dark or light skin. You simply add "d_" in front of name for dark skin.

    For my test I created a window named "Blocks". Note that the icons use the name of the Window, not the class.
    Code (CSharp):
    1. EditorWindow.GetWindow<BlocksWindow>("Blocks");
    So my icons will be:
    - light skin: Assets/Editor Default Resources/Icons/Blocks.png
    - dark skin: Assets/Editor Default Resources/Icons/d_Blocks.png

    window_icons.png
     
    Last edited: Jan 29, 2015
    mgear likes this.
  30. FredZvt81

    FredZvt81

    Joined:
    Apr 8, 2014
    Posts:
    24
    Hi folks!

    Is there a way to change the title and the the icon for a custom editor as well?

    The technique that you are using for custom windows doesn't seem to work with editors. The UnityEditor.Editor base class has no cachedTitleContent property, public nor private, I checked using reflection.

    I'm looking to change at least the title to remove the nasty " (Script)" append to the component title.

    That is a great thread! :)
    Thanks!
     

    Attached Files:

  31. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    That is part of the Inspector rendering in InspectorWindow. I had a quick look and came to a dead-end with a call to an external function GetInspectorTitle(UnityEngine.Object obj) so I do not think this can be changed.
     
  32. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Hey guys,

    I created a utility function to do that a while ago (you can see my personalized Icon in HOTools' screenshot). Check out the SetWindowTitle and GetWinTitleContent methods here.

    hotools.png
     
    col000r likes this.
  33. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
  34. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,085
    I've been testing the "Assets/Editor Default Resources/Icons" method (no reflection, works in 4.x). A few extra notes (tested on 4.5.5):
    • Icons need to be 16x16 (at least for pre 5.4 where retina support is coming). Larger icons expand beyond the edges of the "tab".
    • Textures need their Type set to "GUI (Editor \ Legacy)".
    • If the name of your window is longer than the tab is wide, the icon will not show up. The icon appears to be considered a "nice-to-have" with preference given to the name.
      • If the name of your window is slightly shorter than the width of a tab, the icon will actually be shrunk to fit. This can result in an extra-tiny icon.
    • The name of the icon file must match the name of the window, NOT the name of the class.
    I hope this extra information is helpful to other users looking up how to do this!
     
    Last edited: Dec 29, 2015
    wayofthepigeon likes this.
  35. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    As Jean says; you can just set "titleContent" for your custom editor window.

    I am doing this inside "OnEnable()" for my editor windows and it works perfectly. You can load your icon from any location inside the project's "Assets" folder; decode the image from a base-64 string; or if you are using DLLs you could read from an embedded image resource.
     
    SonicBloomEric likes this.
  36. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,085
    That is true, but this is actually limited to Unity 5.1+. For those looking to support legacy Unity, titleContent is but a tease!
     
  37. tgrotte

    tgrotte

    Joined:
    Apr 17, 2019
    Posts:
    25
    I achieved this with simply:

    [MenuItem("Window/Custom Controls")]
    public static void ShowWindow()
    {
    var window = EditorWindow.GetWindow(typeof(EditorScripting));
    var texture = Resources.Load<Texture>("MyPngImage");
    window.titleContent = new GUIContent("Nice icon, eh?", texture, "Just the tip");
    }

    I additionally made a "Resources" folder under "Assets" and placed the png file I wanted in there.
     
  38. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,085
    Don't do that. The file will end up in your builds.

    You should place it in one of the following locations:
    • Assets/Editor Default Resources/.../MyPngImage.png
    • Assets/.../Editor/.../Resources/.../MyPngImage.png
    where "..." implies any folder hierarchy you want.

    The important parts are that you use either the Editor Default Resources special folder or put your Resources folder under an Editor folder. Both of those options will guarantee that the build process doesn't grab your MyPngImage.png and automatically include it in your build.
     
    tgrotte likes this.
  39. Xan_9

    Xan_9

    Joined:
    Oct 7, 2020
    Posts:
    31
    This simply works without the use of the Resources folder:

    Code (CSharp):
    1. var window = GetWindow<CustomEditorWindow>();
    2. var iconPath = "Assets/icons/window-icon.png";
    3. var icon = AssetDatabase.LoadAssetAtPath(iconPath, typeof(Texture)) as Texture;
    4. window.titleContent = new GUIContent("Custom window name", icon);
     
  40. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,085
    Sure. But why bother? You can use Resources without the usual concern (editor-only images ending up in builds) if you put that folder somewhere under an Editor folder.