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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do i extend the area you can touch around a button

Discussion in 'UGUI & TextMesh Pro' started by trooper, Oct 9, 2015.

  1. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    746
    How do I go about extending the area that allows a button to be pressed outside of the size of the image.

    I don't want to increase the actual sprite size with transparent pixels.
     
  2. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    746
    Figured it out, use Physics2DRaycaster a camera in orthographic mode and add colliders to the ui objects instead of relying on the sprite bounds.
     
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    That's certainly an interesting alternate approach @trooper , hadn't considered that one.
    Fantastic.
     
  4. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    746
    Simon, is there a better one I haven't thought of?

    I ended up grabbing the Physics2DRaycaster code and making a raycaster without RaycastAll to avoid the memory allocations. Seems to work well but I haven't tested on mobile or in a game yet... trying to optimize as much as possible (0 allocations during runtime) before I test on mobile.
     
  5. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    The only other real way would involve having a background transparent image with a script to pick up the click event and either route it to the button or build a new button with a bigger background. Yours was more simplistic. pears and apples :D
     
  6. DWilliams

    DWilliams

    Joined:
    Jan 12, 2015
    Posts:
    63
    What I've been doing for this situation is to have the button image be null with 0 alpha and set it to the size of the clickable button area I want. The actual graphical button image I want to use would simply be a child image on that button and have the button component reference that image for transitions. Probably not so great with extra draw calls but simple to set up and use for a few cases.