I can use the built-in icons in Unity for example like this:
iconToolbarRemoveAll = EditorGUIUtility.IconContent("d_winbtn_mac_close");
which gives me the red close button from OSX. But how can I use my own icons, for example an Icon that I have placed in my assets folder?
1 Like
“EditorGUIUtility.IconContent” just returns a cached GUIContent. A GUIContent is just a data class that groups three things together:
- A text (string)
- An image (Texture)
- A tooltip (string)
If you want to create a GUIContent object with your own image, just create one:
GUIContent icon = new GUIContent(yourTexture);
The constructor of GUIContent has several overloads which allows to use any combination of text image and tooltip. The only thing that isn’t possible to create only a tooltip, but that doesn’t make any sense^^.
You can also change the image, the text or the tooltip after you created the object. You shouldn’t mess around with the built-in GUIContent as they are cached and might be used by other parts of the editor. If you want to modify a built-in GUIContent to use it in your code, create a copy:
yourContent = new GUIContent(builtinContent);
yourContent.image = yourImage;
This would create a copy of the builtin content and then replace the image with your texture.
To load an image from your assets folder, just use any load function that the editor API provides. For example EditorGUIUtility.Load. For this method you should place your image in “Assets/Editor Default Resources/”.
ps:
It looks like EditorGUIUtility.IconContent
just uses EditorGUIUtility.Load under the hood, so it can even load images from your assets folder just by specifing the asset path. I haven’t tried it yet, but it should actually work just fine.