Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Toggle size

Discussion in 'Immediate Mode GUI (IMGUI)' started by smithlim, Jul 27, 2010.

  1. smithlim

    smithlim

    Joined:
    Nov 18, 2009
    Posts:
    21
    Hello~
    I would like to resize toggle checkbox.
    I set the width = 200 and height = 200.
    but the check box size is not changed.

    Code is following.
    bool Value = false;
    void OnGUI()
    {
    bool val;
    Rect Position = new Rect(100, 350, 200, 200);// width = 200, height = 200
    string str = "Toggle";
    val = UnityEngine.GUI.Toggle(Position, Value, str);
    if (val != Value)
    {
    Value = val;
    }
    }

    how to scale up the checkbox itself.
    thanks.
     
  2. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    Unless I am mistaken, the rect argument is for the optional text next to it. I doubt you can resize checkboxes...
     
  3. smithlim

    smithlim

    Joined:
    Nov 18, 2009
    Posts:
    21
    I may control toggle size to update border size of GUIStyle....
     
  4. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    AkilaeTribe is more or less right; the Rect you pass to GUI.Toggle defines the area that the checkbox and label take up together. The checkbox part itself wont scale with the toggle control.

    You may be able to make that bigger by passing GUI.Toggle a GUIStyle with a larger checkbox image (I haven't checked the GUIStyle/GUISkin docs though...)
     
  5. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
  6. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    It'd be the 'background' property of the onNormal/onHover/onActive states, yes. There wouldn't be anything specifically related to toggles in GUIStyle/GUIStyleState, since those classes are used across all GUI controls; there is a GUIStyle for toggles in each GUISkin though.
     
  7. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    !

    If there is already a section for toggle in GUISkin, why are we bothering ourselves with GUIStyle ? :)
     
  8. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    Perhaps I wasn't very clear. the GUIStyle API doesn't mention toggles, because it's a general interface used by all control types to determine style information. Rather, a GUISkin contains an *instance* of GUIStyle that is specific to toggles. Thus:

    • * GUI.skin is the current default GUI skin
      * GUI.skin.toggle is the GUIStyle used by toggles in that skin
    If you want to change the appearance of a toggle control, you can:

    • * pass your own GUIStyle instance into GUI.Toggle (to alter the appearance of just that toggle)
      * modify GUI.skin.toggle (to alter the appearance of all toggles rendered with that skin)
      * set GUI.skin to a different skin (to alter the apperance of all controls, of all types, that you don't override the styles for)

    Hopefully that makes it clearer :)
     
    seobyeongky likes this.
  9. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    In other words, GUIStyles inherit of some values of his parent GUISkin, so even if we give an empty GUIStyle to a GUI.Toggle, as long as the parent GUISkin has the correct images, the GUI.Toggle will get them ?
     
  10. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    No, GUI styles don't inherit anything from anywhere, and a GUI skin doesn't contain any style information by itself; it's just a collection of GUIStyle objects.

    If you call GUI.Toggle (or any GUI control) and pass it a GUI style, it will draw using the setting in that style, and only that style. If you don't pass a style, it'll draw using the settings from the appropriate style in the current skin.

    Code (csharp):
    1.  
    2. GUI.Toggle(rect, val, text); // draw using style 'GUI.skin.toggle'
    3.  
    4. GUI.Toggle(rect, val, text, style); // draw using style 'style'
    5.  
    It's one or the other.
     
  11. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
  12. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    This thread is ancient, but I came across it while searching for a way to do this and thought I'd share the result - inspired by smithlim's "solved" post, I found that by making the following changes to the "toggle" style I can scale the background image.

    - set all four "border" values to zero
    - set all four "overflow" values to zero
    - set "image position" to "image only", and render the label as a separate GUI element instead
    - set "padding.right" and "padding.bottom" to zero
    - set "padding.left" and "padding.top" to the minimum width and height you want the background scaled to

    The first few changes make the background image scale over the whole control's size; getting rid of the label removes that complication. Then the padding settings define a minimum control size, and for most purposes this is enough.

    If you turn off "expand width" and "expand height" then the toggle button will always appear at the size you've specified in the padding settings.

    If you want GUILayout toggles to be able to expand to fill space, for some reason, then turn on "expand width" and/or "expand height". The background image will be scaled across the whole control thanks to the "border" settings. You can probably get the same effect by using the GUILayoutOption variants on the actual GUILayout.Toggle call.
     
    Andrew900460, Xitech_ and Graph like this.