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
8
Question by Denmark111 · Jan 22, 2015 at 03:41 PM · ui4.6

How to stop non-rectangular buttons from overlapping?

I'm trying to figure out how to use Unity's new UI system, and I've run into a problem. It seems that it only allows rectangles to be buttons.

What I want to do is have a circle sliced into 7 parts, where each part is clickable (see image). However, when I attempt this, the rectangle of the button of one slice overlap the images of other slices.

Is there any way to fix this using the UI system of Unity?

alt text

round menu.png (12.7 kB)
Comment
Add comment · Show 2
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 team_eden · Jan 22, 2015 at 09:52 PM 0
Share

You should do a mouseover to check which image is Actually underneath the mouse, and then just lock that Option of THAT image as the only one that can be clicked, thus ignoring any overlap. Hope that helps

avatar image Denmark111 · Jan 23, 2015 at 10:14 AM 0
Share

I do not understand what you're telling me. How do you do a mouseover check in Unity UI?

4 Replies

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

Answer by troien · Jan 23, 2015 at 10:28 AM

For some reason (don't ask me why) the faq of Unity UI is almost unknown to google, while it has a lot of information that is rather usefull :p

For you, this part is something I think you are looking for:

How do I make UI elements have non-rectangular hitboxes?

Add a component that implements ICanvasRaycastFilter. In your implementation of IsRaycastLocationValid, reject all points that are 'outside' your custom hitbox shape. It's up to you how you want to do that, but for example for a circular hitbox you could measure the distance between the point and your object's center and reject the point if that distance is greater than your circle's radius. Or perhaps you could look up the alpha value of the pixel the hit is on, as per this script by senritsu.

Sadly, the link over there is broken :( However, using google I was able to find this...

Forum post: http://forum.unity3d.com/threads/none-rectangle-shaped-button.263684

Download link: http://forum.unity3d.com/attachments/raycastmask-cs.110370/

Comment
Add comment · Show 2 · 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 Denmark111 · Jan 23, 2015 at 10:49 AM 0
Share

I have no clue what the linked code says, but it works. Thanks a lot for your googling skills :)

avatar image Tomer-Barkan · Sep 29, 2015 at 04:46 AM 0
Share

This is what I used too. Too bad Unity didn't bother documenting IRaycastFilter

avatar image
17

Answer by miguelSantirso · Jun 06, 2015 at 04:38 PM

I found another (probably better) solution for this problem. I used a Collider2D (PolygonCollider2D) to define the clickable shape and then used this RaycastFilter:

 using UnityEngine;
 using UnityEngine.UI;
  
 [RequireComponent (typeof (RectTransform), typeof (Collider2D))]
 public class Collider2DRaycastFilter : MonoBehaviour, ICanvasRaycastFilter 
 {
     Collider2D myCollider;
     RectTransform rectTransform;
     
     void Awake () 
     {
         myCollider = GetComponent<Collider2D>();
         rectTransform = GetComponent<RectTransform>();
     }
  
 #region ICanvasRaycastFilter implementation
     public bool IsRaycastLocationValid (Vector2 screenPos, Camera eventCamera)
     {
         var worldPoint = Vector3.zero;
         var isInside = RectTransformUtility.ScreenPointToWorldPointInRectangle(
             rectTransform,
             screenPos,
             eventCamera,
             out worldPoint
         );
         if (isInside)
             isInside = myCollider.OverlapPoint(worldPoint);
         return isInside;
     }
 #endregion
 }
 

Using a collider for this is a bit hacky but the result is much simpler, probably faster and makes it much easier to adjust the clickable shape.

This could be improved by creating a custom alternative to replace the Collider2D but that's a bit too much work for what I need.

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 ecesis_llc · Sep 29, 2015 at 03:02 AM 3
Share

Thanks for this little gem. To make use of this script:

  1. Create a new script and paste in this content.

  2. Add a 2d collider component to your game object

  3. Add the script to the game object

Would be great if Unity just changed the way the hit box functioned based on if there is a 2d collider component on your button.

Thanks for the filter.

avatar image Usmith · Mar 24, 2016 at 03:14 AM 0
Share

I think this is the best way to archive this kind of purposes.

avatar image Platoon666 · Dec 11, 2016 at 03:54 PM 0
Share

This works pretty well. Thanks.

avatar image alex_netsov · May 23, 2017 at 02:02 PM 0
Share

Still works like a charm!

avatar image MrMetwurst2 · Jul 19, 2017 at 11:52 AM 0
Share

This script is awesome. Simple but accomplishes the task perfectly! Thank you so much!!

Show more comments
avatar image
0

Answer by rob5300 · Jan 22, 2015 at 09:55 PM

The only way i can think that you can do this is by not using the new UI and using a system of another camera, and Sprites which have custom scripts to detect clicks and hovers using OnMouseOver and OnMouseDown. (Make sure the sprites have 2d colliders too!

Soo for example:

 using System.Collections;
 using UnityEngine;
 
 public class Example : MonoBehaviour {
     //This may or may not work, this is just to illustrate.

     public Color MouseOverColor;
     public Sprite sprite;

     private Color startColor;

     void Start(){
         startColor = sprite.color;
     }
 
     void OnMouseOver(){
     //For example change the button color when moused over
         sprite.color = MouseOverColor;
     }
     
     void OnMouseExit(){
     //To revert the color on the mouse exiting.
         sprite.color = oldColor;
     }
     
     void OnMouseUpAsButton(){
         //This is called when the mouse is released over the same
         //collider as it was pressed on, great for buttons.
         DoThing();
     }
 }

Comment
Add comment · Show 4 · 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 Denmark111 · Jan 23, 2015 at 10:12 AM 0
Share

This would work using raycasting, yea? (thanks for your reply btw)

avatar image rob5300 · Jan 23, 2015 at 10:18 AM 0
Share

No problem! And Yep, i gave the examples of using the mouse events in case you didn't wish to use raycasting. You may have to use Physics2D.Raycast for 2d sprite colliders, but try the normal one first!

avatar image jniris · Mar 30, 2015 at 05:03 PM 0
Share

Hi can you give more details about the mouse actions? Thanks

avatar image Denmark111 · Apr 11, 2015 at 09:03 AM 0
Share

@jniris I would recommend you writing a new question and provide some more details about what actions you want to know about therein. Or, alternatively, try making a forum post if you can't think of a way to make your question more specific. UnityAnswers is more of a "I have a problem, please give me a solution"-kind of site.

avatar image
0

Answer by menehune23 · Oct 19, 2015 at 10:43 AM

I recently came across this and found the answers here on this forum to be a great starting point! I consolidated and updated the info into a concise blog post:

Non-Rectangular UI Buttons in Unity

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

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

16 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

Related Questions

Infinite ScrollRect? 1 Answer

Getting on screen sizes of a UI element in Unity 4.6 (while using CanvasScaler),Get the real size on screen of an UI element in Unity 4 0 Answers

Instantiating UI elements in 4.6 2 Answers

Left end of ui slider background flattens when moving handle to left 0 Answers

4.6 UI Dynamic button event system PointerEnter, PointerExit, PointerUp etc. 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