Search Unity

Editor button with texture?

Discussion in 'Immediate Mode GUI (IMGUI)' started by dogzerx2, Aug 7, 2013.

  1. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,967
    I'm working on an editor thing, and I wanted to use images for the buttons.

    But I'm not sure how to access textures from the script. Since it's an editor script, I can't drop the texture on the script.

    I've been looking around and it seems I have to use the AssetDatabase, but it's a little confusing.
     
  2. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Why not use a Texture2D and reference it like you would from a GUIButton?
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    I used this to load images (Texture) :
    Code (csharp):
    1. Resources.Load("imagename") as Texture;
    and images are in Resources/ folder
     
  4. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Code (csharp):
    1. private Rect _rect = new Rect(10, 10, 100, 40);
    2. private GUIContent _content;
    3.  
    4. void OnGUI() {
    5.     if (null == _content)
    6.         _content = new GUIContent("Click me", (Texture)Resources.Load("your_image"); // file name in the resources folder without the (.png) extension
    7.     GUILayout.Button(_rect, _content);
    8. }
     
  5. GearKlik

    GearKlik

    Joined:
    Sep 21, 2015
    Posts:
    58
    Old thread but I just had a lot of trouble with this and got it working.

    Code (CSharp):
    1. // Define a texture and GUIContent
    2. private Texture button_tex;
    3. private GUIContent button_tex_con;
    4. ...
    5. // Load the texture resource
    6. this.button_tex = (Texture)AssetDatabase.LoadAssetAtPath("Assets/folder/folder/myButton.png", typeof(Texture));
    7.  
    8. // Define a GUIContent which uses the texture
    9. this.button_tex_con = new GUIContent(button_tex);
    10. ...
    11. // Use it in a button, this is sumple formatting for example
    12. GUILayout.Button(button_tex_con, GUILayout.Width(100), GUILayout.Height(100))