Adding images to EditorWindow scripts

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"		
	
    }

}

thanks!

-Dan

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:

Assets/MyPackage/Runtime/ %runtime stuff%
Assets/MyPackage/Editor/ %editor stuff%

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:

public class TestLogo : EditorWindow
{
    [MenuItem ("TESTS/TestLogo")]
    static void Init ()
    {
        TestLogo window = (TestLogo)EditorWindow.GetWindow (typeof(TestLogo));
    }

    private Texture2D m_Logo = null;
    void OnEnable()
    {
        m_Logo = (Texture2D)Resources.Load("logo",typeof(Texture2D));
    }

    void OnGUI ()
    {
        GUILayout.Label(m_Logo);
    }
}

So load the image into a variable on the class with

  Texture2D image;

   void Awake()
   {
       image = Resources.Load("somePath", typeof(Texture2D)) as Texture2D;
   }

   void OnGUI()
   {
         GUILayout.Label(image); //Or draw the texture
   }

I know this is an old question but I would like to suggest using the LoadAssetAtPath method of the AssetDatabase.

Here is an example:

GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/your_path/your_file.prefab", typeof(GameObject));

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.

cheers!

-Dan