Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by Helical · Dec 01, 2014 at 11:33 AM · uibutton

UGUI UI, How to increase Hitzone, Click area, Button Rect, respectivlly to the Image component?

What is the fastest most accurate (platform independent) way to do that?

My personal thoughts - (I could just edit the image with more dead space around it, but thats a very bad solution. I could create a separate button without a zero alpha and a big hitzone, and just place an image underneath it, However then the transition effects that we get from the Button component can't be used. Maybe there is a way that a click on one button will activate a click on another button?).

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Louis-N-D · Dec 01, 2014 at 05:22 PM 0
Share

I have no better solution for you, but, given how pervasive Unity is on mobile devices, I am sad that the uGUI buttons don't simply have a way of defining a bigger touch area than the visual button.

7 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by Eliot_L · May 04, 2015 at 11:25 PM

I've found a solution for an invisible button that doesn't add unnecessary alpha to the scene. First, add this script to your project:

 //EmptyGraphic.cs
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 public class EmptyGraphic : Graphic {
     protected override void OnFillVBO(List<UIVertex> vbo)
     {
     }
 }

Then make a new button, delete the Image component, and add the EmptyGraphic component. Then hook up the Button's Target Graphic as described by Helical. Works like a charm!

Shout out to @ragzouken who gave me this tip.

I was also having trouble making adaptive layouts, but I found a solution to that too by making the invisible button a sibling of the visible button instead of a parent. Make sure it's beneath the visible button in the hierarchy, otherwise the visible button will eat clicks where it overlaps.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Helical · Oct 21, 2015 at 07:43 AM 2
Share

some other comment updated for Unity 5.2

 //EmptyGraphic.cs
  using UnityEngine;
  using UnityEngine.UI;
  using System.Collections;
  using System.Collections.Generic;
  
  public class EmptyGraphic : Graphic {
      protected override void OnPopulate$$anonymous$$esh($$anonymous$$esh m)
      {
          m.Clear(false);
      }
  }

avatar image KirillKuzyk · Jan 03, 2016 at 02:54 PM 0
Share

for Unity 5.3:

 using UnityEngine;
 using UnityEngine.UI;
 public class EmptyGraphic : UnityEngine.UI.Graphic
 {
     protected override void OnPopulate$$anonymous$$esh(VertexHelper vh)
     {
         vh.Clear();
     }
 }
avatar image okankayhan · Nov 21, 2016 at 02:24 PM 0
Share

edit: Works perfectly, saves you some valuable draw calls !

avatar image
36

Answer by gselfish · Jun 02, 2015 at 10:51 AM

Just figured this out today, apparently the best way to increase the collision area of a button is to add a UI Text element as a child.

Unlike UI Image, Text can be left blank and freely resized, creating an invisible collider box without any alpha overhead.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flyingbanana · Oct 31, 2015 at 05:33 PM 0
Share

This seems to be the best method for me! I currently have button image as a child, and an empty text element attached to the button. Works exactly as desired without any extra scripts!

avatar image ZannaU · Jan 14, 2016 at 07:07 PM 0
Share

This is the most elegant and easy to implement solution. Please change best answer to this one.

avatar image callen · Feb 26, 2016 at 08:20 PM 0
Share

Great solution, thanks a lot!

avatar image Anisoropos · Mar 06, 2016 at 08:15 PM 0
Share

Turned this into a script for convenience:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class LargeButton : $$anonymous$$onoBehaviour {
 
     public int extraPixels;
 
     public Text expansion;
 
     // Use this for initialization
     public void Expand () {
         if (!expansion)
         {
             GameObject temp = new GameObject("Expansion");
             temp.Reparent(transform, true);
             expansion = temp.AddComponent<Text>();
         }
 
         expansion.text = "";
         RectTransform rT = expansion.GetComponent<RectTransform>();
         rT.anchor$$anonymous$$in = Vector2.zero;
         rT.anchor$$anonymous$$ax = Vector2.one;
 
         rT.sizeDelta = Vector2.one * extraPixels;
     }
 }
avatar image EvanCheddar · Apr 12, 2018 at 03:44 PM 0
Share

even in 2018, this is still the easiest and probably the best solution. I will say that if you're using your button as a way to fire your weapon and want to have limited "ammo" or "projectiles" in your inventory, you must turn off your "raycast target" via code, otherwise once your inventory hits "0" it'll go into the negative number and still fire. hitbox.GetComponent ().raycastTarget = false; something like this in the update will turn it off if inventory hits "0".

Show more comments
avatar image
6

Answer by booferei · Sep 14, 2017 at 07:56 PM

Combing all the best from the answers before me, here's an easy fix-em-all solution. Just drop this script on a button - no need to change your layout.

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class ButtonHitZone : MonoBehaviour {
 
     public float width;
     public float height;
 
     public class EmptyGraphic : Graphic {
         protected override void OnPopulateMesh(VertexHelper vh) {
             vh.Clear();
         }
     }
 
     void OnValidate() {
         RectTransform rectTransform = GetComponent<RectTransform>();
         if (rectTransform != null) {
             width = Mathf.Max(width, rectTransform.sizeDelta.x);
             height = Mathf.Max(height, rectTransform.sizeDelta.y);
         }
     }
 
     void Awake() {
         createHitZone();
     }
 
     void createHitZone() {
         // create child object
         GameObject gobj = new GameObject("Button Hit Zone");
         RectTransform hitzoneRectTransform = gobj.AddComponent<RectTransform>();
         hitzoneRectTransform.SetParent(transform);
         hitzoneRectTransform.localPosition = Vector3.zero;
         hitzoneRectTransform.localScale = Vector3.one;
         hitzoneRectTransform.sizeDelta = new Vector2(width, height);
 
         // create transparent graphic
         // Image image = gobj.AddComponent<Image>();
         // image.color = new Color(0, 0, 0, 0);
         gobj.AddComponent<EmptyGraphic>();
 
         // delegate events
         EventTrigger eventTrigger = gobj.AddComponent<EventTrigger>();
         // pointer up
         addEventTriggerListener(eventTrigger, EventTriggerType.PointerDown,
             (BaseEventData data) => { ExecuteEvents.Execute(gameObject, data,
                                         ExecuteEvents.pointerDownHandler); });
         // pointer down
         addEventTriggerListener(eventTrigger, EventTriggerType.PointerUp,
             (BaseEventData data) => { ExecuteEvents.Execute(gameObject, data,
                                         ExecuteEvents.pointerUpHandler); });
         // pointer click
         addEventTriggerListener(eventTrigger, EventTriggerType.PointerClick,
             (BaseEventData data) => { ExecuteEvents.Execute(gameObject, data,
                                         ExecuteEvents.pointerClickHandler); });
     }
 
     static void addEventTriggerListener(EventTrigger trigger, EventTriggerType eventType,
                                          System.Action<BaseEventData> method) {
          EventTrigger.Entry entry = new EventTrigger.Entry();
          entry.eventID = eventType;
          entry.callback = new EventTrigger.TriggerEvent();
          entry.callback.AddListener(new UnityEngine.Events.UnityAction<BaseEventData>(method));
          trigger.triggers.Add(entry);
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xdotcommer · Jun 18, 2018 at 04:50 AM 0
Share

Niiiiiiice

avatar image Grinning-Pickle · Jun 19, 2020 at 12:20 PM 1
Share

I've added some Gizmo drawing to this script for better usability. Unfortunately it exceeds the character limit of a reply so it's on pastebin. Hope this can help someone.

ButtonHitZone (improved) on Pastebin

avatar image bettywhite Grinning-Pickle · Apr 03 at 05:15 AM 0
Share

Just dropping by to say thank you and nice work to @booferei and @Grinning-Pickle !

Also, looks like you need to add the following to CreateHitZone(): //add canvasRenderer gobj.AddComponent();

avatar image
3

Answer by Helical · Dec 01, 2014 at 05:48 PM

I have found What I was looking for, but maybe there is a better way out there. Here is the procedure and where the cat kicks the bucket.

Create a Button UI element somewhere and set the alpha of the Image component of the Button element to zero, resize the element to the size of the desired Hitbox( If you remove the Image component from the Button Element then it will use the Hitbox of the sub Image that we are going to add).

next create an Image Element underneath (a child in the transform hierarchy) the Button Element. And set it to the desired size of the visual image.

and lastly attach the child image as the Target Graphic in the parent Button Component.

The trick here is to have an Image component on both the Button UI element, and its child Image, and set the Target Graphic on the Button to the child image.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Louis-N-D · Dec 01, 2014 at 06:01 PM 0
Share

Ah! Yes that looks like it would work. Good job, man!

avatar image Helical · Dec 01, 2014 at 06:43 PM 0
Share

Its still feels like a little lengthy procedure, I hope the uGUI $$anonymous$$m will do this by default ins$$anonymous$$d of giving us a text as a child by default (which I always erase)

avatar image WookieWalker · Jan 08, 2015 at 08:21 PM 0
Share

Beautiful! Been looking for something like this for ages now and its been right in my face the whole time lol. Gj.

avatar image elhispano · Jan 20, 2015 at 10:34 AM 0
Share

They should allow to modify the clickable area freely. $$anonymous$$aybe a new component that overrides the default area could be a good solution.

avatar image Louis-N-D · Jan 20, 2015 at 11:20 AM 0
Share

I totally agree with elhispano. Basically, every mobile project I've worked on had needed this functionality somewhere.

Show more comments
avatar image
2

Answer by Meceka · Jan 28, 2015 at 03:31 PM

I am just creating a new UI image, making it child of the UI element that I wish to set hitbox, set new image's alpha as 0, and that image works as a hitbox.

Edit: Use an empty UI Text as child instead. My (old) solution causes unnecessary overdraw.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

49 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simulate Button presses through code unity 4.6 GUI 4 Answers

Adding child to Canvas 1 Answer

uGUI UI Button onclick static methods 3 Answers

Interact with uGUI button using code? 1 Answer

Rotate character with button hold in uGUI? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges