Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Load a texture in editor extension?

Discussion in 'Scripting' started by Alan47, Feb 18, 2013.

  1. Alan47

    Alan47

    Joined:
    Mar 5, 2011
    Posts:
    163
    Hello everyone,

    I'm currently trying to load a simple .png texture into my editor extension. The problem is that all means to do so I've found via Google fail completely. What I've tried so far:

    Code (csharp):
    1. private Texture2D texture = (Texture2D)EditorGUIUtility.Load("assetName.png");
    Code (csharp):
    1. private Texture2D texture = (Texture2D)AssetDatabase.LoadResourceAtPath("<asset path>/assetName", typeof(Texture2D));
    Code (csharp):
    1. private Texture2D texture = (Texture2D)Resources.Load("<asset path>/assetName");
    ... and all of them return "null". I've tried all versions with and without file extensions, and yes, my file paths were correct.

    For the first version, I'm not entirely sure where the asset actually has to reside and I'd be happy to have any clarification which of these is valid (please note the subtle differences in spacing and folder structure):
    • Assets/EditorDefaultResources/assetName.png
    • Assets/Editor DefaultResources/assetName.png
    • Assets/Editor Default Resources/assetName.png
    • Assets/Editor/DefaultResources/assetName.png
    • Assets/Editor/Default Resources/asset.png

    According to this documentation page, it should be the third option, but it's not working for me...

    Am I overlooking a mistake here or is there a bug in Unity 4?


    Thanks,


    Alan
     
    IgorAherneBusiness likes this.
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,999
    Atleast this works when loading button images,

    private static Texture testButton = new Texture;

    ..

    Code (csharp):
    1. testButton = Resources.Load("mytexture") as Texture;
    Try without the file extension?
    (and I had those images on "Assets/Resources/" folder.
     
    rsodre likes this.
  3. Alan47

    Alan47

    Joined:
    Mar 5, 2011
    Posts:
    163
    @mgear: Yes, you are right - this combination *does* work! It seems that this is one I did not try so far. It is a little odd, because I remember that I read somewhere during my research that "Resources.Load" in principle should not even be available from the editor. It seems that this information was wrong.

    Thanks a lot for your hint, you really helped me out on this one :)


    Alan


    PS: @developers: I think the scripting API documentation would deserve an update on this topic...
     
  4. yoyo

    yoyo

    Joined:
    Apr 16, 2010
    Posts:
    112
    I wanted to load textures into my editor extension too. As my extension is a DLL, I thought it would be nice to bundle the textures as resources embedded in the DLL. To get the textures to load, I did the following. Note that the texture will be destroyed on a code reload in the Editor, so you will need to reload it after editor play mode changes.

    Code (csharp):
    1.  
    2.         private static Texture2D LoadTexture(string fileName)
    3.         {
    4.             Texture2D texture = new Texture2D(0, 0);
    5.  
    6.             // Note that resource path is dependent on assembly namespace and source folder layout.
    7.             var resourcePath = string.Format("Namespace.Folder.Path.{0}", fileName);
    8.             var assembly = System.Reflection.Assembly.GetExecutingAssembly();
    9.             using (var stream = assembly.GetManifestResourceStream(resourcePath))
    10.             {
    11.                 if (stream == null)
    12.                 {
    13.                     Debug.LogError(string.Format("{0}: GetManifestResourceStream failed", resourcePath));
    14.                     return null;
    15.                 }
    16.  
    17.                 byte[] buffer = new byte[stream.Length];
    18.                 stream.Read(buffer, 0, buffer.Length);
    19.                 if (!texture.LoadImage(buffer))
    20.                 {
    21.                     Debug.LogError(string.Format("{0}: LoadImage failed", fileName));
    22.                     return null;
    23.                 }
    24.             }
    25.  
    26.             return texture;
    27.         }
    28.  
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Myself, I had issue with texture loading from DLL because it kept trying to link to System.Drawing, which on Mac were highly problematic. Since our library had to work on Mac and PC, we package the .png as binary. (copy paste file, rename extension to .binary :p)

    We load them doing;

    Code (csharp):
    1.  
    2. public class Helper
    3. {
    4.     public static Texture Load(string name)
    5.     {
    6.         Texture2D texture = new Texture2D(256, 256);
    7.         texture.LoadImage((byte[])EditorResources.ResourceManager.GetObject(name));
    8.         texture.filterMode = FilterMode.Point;
    9.         return texture;
    10.     }
    11. }
    12.  
    example of using that library;

    Code (csharp):
    1.  
    2.     private static Texture folderOpen;
    3.  
    4.     public static Texture FolderOpen
    5.     {
    6.         get
    7.         {
    8.             if (folderOpen == null)
    9.                 folderOpen = Helper.Load(EditorResources.FolderOpen);
    10.  
    11.             return folderOpen;
    12.         }
    13.     }
    14.  
     
  6. Tehelee

    Tehelee

    Joined:
    Jan 26, 2013
    Posts:
    12
    Code (CSharp):
    1. Texture2D myTexture = new Texture2D( 1, 1 );
    2. myTexture.LoadImage( System.IO.File.ReadAllBytes( "<asset path>/assetname.png" ) );
    3. myTexture.Apply();
    This will work for PNG and JPG, other file types are untested.
    This works in Editor, and in-play mode on OSX and Windows.