Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GUI.Button: define custom hover Rect?

Discussion in 'Immediate Mode GUI (IMGUI)' started by perfect440, Dec 30, 2007.

  1. perfect440


    May 7, 2007
    I'm trying to get a button working which consists of a small image and some text. Right now the button only changes hover states when the mouse is over the image, but I want the text to be included in this hover area.
    Changing the rect width in the GUI.Button call only streches the image. I found that by setting the Fixed Width parameter in the Style, I can bypass thh stretching while keeping the hover Rect wider, but this seems a bit contrived, and also doesn't work when the button text is on the left of the image.

    Maybe I'm missing something really simple. I want to specify a custom Rect for a button (which I can already do now, but...) while keeping the image in current size/proportions, and also specify a position offset for that image relative to the rect origin (much like the Content Offset now).

    Whew, I hope that made sense. Thanks y'all.
  2. shaun


    Mar 23, 2007
    How about making a GUIContent variable that stores the image and text, and using that in the button. Then set the buttons GUIStyle.none (to remove the button image).
    The shortcoming would be that only the text color would change on Hover - the image would stay the same.

    The only other way around this is to use the same technique used with the Toggle control (check the border values in the GUISkin to see what I mean). It does mean the images have to be a fixed width/height - but you get nice rollover effects :)
  3. Joe ByDesign

    Joe ByDesign

    Oct 13, 2005
    You can achieve this by setting the Fixed Size for the button's GUIStyle to match the image size, then in the GUI.Button call, set the Button's rect's width height to larger than the image (to cover the text).

    The result is that button image will appear as desired (via Fixed Size), but the hover area will encompass the Button's rect width height.
  4. NicholasFrancis


    Apr 8, 2005
    It sounds a bit like you're basically trying to put an image on a button, correct?

    If so, you can use
    Code (csharp):
    2. GUI.Button (new Rect (10,10, 100, 20), new GUIContent (text, image));