Search Unity

Newb having problems with images in GUI.Box

Discussion in 'Immediate Mode GUI (IMGUI)' started by degeneration, May 13, 2008.

  1. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    Hi Everybody,

    Just starting out in Unity after years of Director3dness and loving the potential of Unity, but I've hit a bit of a stumbling block. I've been following http://unity3d.com/support/documentation/Components/gui-Basics.html to create buttons, which work fine, but when I try to add an image nothing happens.

    My script...
    Code (csharp):
    1. var bgImage : Texture2D;
    2.  
    3. function OnGUI(){
    4.     GUI.Box(Rect(0,0,256,256),GUIContent("Select a shape",bgImage));
    5.     if(GUI.Button(Rect(55,40,80,20),"Box")){
    6.         print("Box");
    7.     }
    8.     if(GUI.Button(Rect(55,70,80,20),"Sphere")){
    9.         print("Sphere");
    10.     }
    11. }
    12.  
    Now, if I've understood this I assign an image by dragging an image asset from the project window into the default references. It seems to relate bgImage to the image I want to use, but I can't see anything.
    The image is a psd 256x256.

    Any ideas what I'm doing wrong or any idiot safe tutorials on GUI?

    Sorry if this seems dumb.
     
  2. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
  3. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    Didn't seem to make a difference. No image is shown. The text displays, but the image doesn't. I've tried a jpeg and a psd. The JPEG was 32x32, the psd 256x256. I've tried Texture and Texture2D as the var. With and without GUIContent. But nothing.

    It seems like it should be pretty straight forward.
     
  4. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    Not sure why that's not working. (I haven't worked with the GUI.Content constructor)

    Another option is to install a custom skin and assign your texture to the GUISTyle of the box in the inspector. (The skins are on the wiki and have been posted in threads here.)
     
  5. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Your script worked for me when I tried a 256 x 256 .PNG:
     

    Attached Files:

  6. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    I'll be a monkeys uncle... the png worked...

    Thanks bigkahuna!

    Are there certain filetypes to avoid/prefer for gui?
     
  7. degeneration

    degeneration

    Joined:
    Apr 3, 2008
    Posts:
    115
    Ah... I may have discovered my error. If I have this right, I need to assicoiate the image with the instance of the script on the gameobject rather than the instanc e of the script in the project window... If that makes sense?
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Avoid .jpg, but everything else should work. (.jpg will work, but you don't want the quality loss.)

    --Eric
     
  9. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Welcome aboard then. :D

    That does make sense as it's how you do things. :)