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.

GUI Bug?

Discussion in 'Immediate Mode GUI (IMGUI)' started by yellowlabrador, Nov 3, 2007.

  1. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    Hello all,

    Not sure if this is a bug or me doing something wrong.

    Code (csharp):
    1.  
    2. var customSkin : GUISkin;
    3. var ourImage : Texture;
    4.  
    5. function OnGUI () {
    6.     GUI.skin = customSkin;
    7.  
    8.     if(GUI.Button(Rect(Screen.width/2-100,Screen.height/2-100,200,200), ourImage))
    9.     {
    10.     print("test");
    11.     }
    12.  
    13.    
    14.     // Now create any Controls you like, and they will be displayed with the custom Skin
    15.     //GUI.Button (Rect(Screen.width/2-100,Screen.height/2-100,200,200),ourImage);
    16.    
    17.  
    18.     // You can change or remove the skin for some Controls but not others
    19.     GUI.skin = null;
    20.  
    21.     // Any Controls created here will use the default Skin and not the custom Skin
    22.     GUILayout.Button ("This Button uses the default UnityGUI Skin");
    23. }
    24.  
    the code is from the docs, I made little changes to it but the prob I have is in the inspector, when I add/drag my texture to the inspector(ourImage var) I get double Image, if I leave the var ourImage blank (ref.unassigned and no errors), the gui is fine, see attached image.

    Not sure if its a bug or just me not doing it right. sorry just playing with the new GUI system.

    Thanks,
    Ray
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,711
    I suspect it's due to combining GUI.Button and GUILayout.Button - I don't think they mix well, and you've got both in there.
     
  3. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    Even If I remove the GUILayout same prob.

    Not assigning a texture reference in the inspector(declared as global var or public? in the script) is not giving any refence not set to instance error.

    I'll re read the manual on this GUI stuff again, maybe I just missed something as always :).

    Ray
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    You're assigning your texture as GUIContent and as a GUIStyle.
    I can't figure out exactly what you want to do, but I think the solution may be to create a custom GUI Skin, set it up correctly with all the button images first, and just leave the content blank.
    i.e.
    Code (csharp):
    1. GUI.skin = myCustomSkin; //Use your custom Skin
    2. GUI.Button(new Rect(0,0,100,32), "");
    3. GUI.skin = null; //Use the Unity Skin
    4. GUI.Button(new Rect(0,40,100,32),"");
    The point is to setup the background texture for the button in the skin at design time - at runtime when you create a button using GUI or GUILayout it will just work.

    You can script custom GUIStyles at runtime, but it might be better to get the hang of things first.

    @StarManta
    It's perfectly OK to mix GUILayout and GUI together - and is sometimes necessary.

    hope this helps
    Shaun
     
  5. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    Shaun,

    Sorry, just started playing with the GUI stuff, basically I just wanted to create a custom button(for now) that a user can press and it will activate an event. Your Script did work , I just adjusted the size of the button to make the image more readable.

    Question, How do I adjust the size of the button of the GUI?
    Right now when I hover the mouse over the GUI it will render the onHover image even when the mouse is not on top of the GUI. See image below GUI1 (Bottom Image) is normal mode and GUI2 (top Image) is when I hover the mouse over. The side of the GUI hovers fine(mouse at the edge and hover image activates), top and bottom only, tried playing with borders and margins set to 0 but not working.

    Thanks,
    Ray
     

    Attached Files:

  6. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    The size is controlled by the Rect(X,Y,Width,Height).
    It looks like your button is a fized sized graphic so you just need to set the Width and Height part of the Rect to match the image dimensions.
    If you want to make resizable buttons, you need to use the border settings, which control which part of your image is stretched and which parts remain "pixel perfect".

    I think you may benefit from doing the GUI tutorial - especially the part on creating custom GUI skins. FYI a GUI Skin is just a design time collection of GUISkin settings and GUIStyles that can all be created and controlled via scripting.

    http://unity3d.com/support/documentation/Components/gui-Customization.html

    Also important to note is that the GUI is immediate mode, which means that changes made inline in the code will affect whatever you GUI code you call afterwards. Therefore, if you specify GUI.skin = null (which means use the built in Unity Skin) it will continue to use that skin for all remaining controls that get rendered.

    hope this helps - the GUI can be tricky at first.
     
  7. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    Sorry not to respond right away, just got some free time today,

    Yup, texture size is 1024 by 512. Why that big, I could probably make it smaller but I use this and others on a plane so I can have some mouse over feature(audio etc). Trying to use the new GUI to replace my own generic gui(buttons) that I am using on world capitals game.

    Is there a specific size that a texture image should have to be used as a gui background, so it will fit nicely. sorry rambling over here :)

    Will experiment with that one and the other controls (wrap, etc)

    Very tricky... would love to have some more tutorials on it though ;-)

    Thanks,
    Ray
     
  8. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    A need we recognize and will hopefully get to sooner as opposed to later.