I would like to include images in my custom editor windows but cannot seem to figure out the approach to do this. I have the following script example that shows were I would want to do this.
using UnityEditor;
using UnityEngine;
using System.Collections;
public class TestLogo : EditorWindow {
[MenuItem ("TESTS/TestLogo")]
static void Init () {
TestLogo window = (TestLogo)EditorWindow.GetWindow (typeof(TestLogo));
}
void OnGUI () {
GUILayout.Label("I want an image here");
// This is where an image would be used that is in my project.
// ie "Assets/Resources/icon.png"
// ie "Editor/icon.png"
}
Keep in mind that assets placed in the Resources folders are always included into the build, no matter if they are used or not. Even when you place a Resources folder in an “Editor” folder, the assets will be included in the build (just tried it ;)).
Unfortunately there’s no elegant way to load editor-related resources. You can use Resources.LoadAssetAtPath which is an editor only function that can be used to load an arbitrary asset (it doesn’t have to be in a Resources folder) by it’s relative asset path. However that requires the user of your editorwindow to not move the files inside the project. So if you use this method, use a meaningful folder “Assets/Editor/Images/” for example. If it’s a special package that contains both, runtime scripts and editor stuff you might want to group ot like this:
It’s a pity there’s not something like a EditorResources folder which is not included in the build, but you can use Resources.Load on them. I don’t like the Resources folder and i always try to avoid it in projects, however for editor stuff it would be perfect (since default references doesn’t work anymore).
Another way would be to use System.IO to search for the resources manually.
Btw. Editors or EditorWindows also have a OnEnabled and OnDisable callback which should be used to initialize your window / editor. this works:
Thanks you very much…that was exactly what I needed. I had figured out a bunch of other stuff in learning to make Editor Scripts but loading an image was giving me a real headache.