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
10
Question by Screenhog · Oct 31, 2014 at 06:07 PM · ui

(Unity 4.6) Is it possible for UI buttons to be non-rectangular?

I'd like to have buttons that are not rectangular. Circle, hexagon, odd shapes... is this possible? I'd heard rumors that this can work with masking, but I haven't observed this to be true.

Comment
Add comment
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

6 Replies

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

Answer by Elringus · Nov 07, 2015 at 03:26 PM

Have a look to this asset: https://www.assetstore.unity3d.com/en/#!/content/28601

It checks texture transparency, before deciding whether to send input events for targeted object, which allows a precise interaction with objects that have a non-rect shapes (like circles, triangles, map regions, etc). That way you will be able to make buttons and other interactable UI objects of any shape and they will correctly react to user input.

Comment
Add comment · Show 10 · 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 Fattie · Feb 19, 2016 at 03:14 PM 1
Share

this thing is called Alpha Raycaster

IT IS AWESO$$anonymous$$E, BUY I$$anonymous$$$$anonymous$$EDIATELY

it is completely supported and updated every few weeks. it's simply a must have until Unity build-in this functionality.

avatar image supershwa · Apr 19, 2017 at 03:07 PM 18
Share

You don't need to purchase an asset from the store to accomplish this. It's quite easy (in Unity 5.5). You use alphaHitTest$$anonymous$$inimumThreshold so Unity ignores the alpha pixels of your image as "hit" zones. I use a small simple script I attach to button components.

First, create a C# script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI; // Required when Using UI elements.
 
 public class AlphaButton : $$anonymous$$onoBehaviour
 {
     public float AlphaThreshold = 0.1f;
 
     void Start()
     {
         this.GetComponent<Image>().alphaHitTest$$anonymous$$inimumThreshold = AlphaThreshold;
     }
 }


Attach this new script to the button component you wish to have a custom hit zone.

Then, you will need to edit your image import settings (your button image) and under Advanced, make sure Read/Write Enabled is checked.

Now, go back to your button component, and in the AlphaButton component in the inspector you can adjust the alpha threshold for your image -- currently set to 0.1f. This value defines the $$anonymous$$imum alpha level of a pixel in your button that qualifies as a "hit zone".

It takes about 60 seconds to apply! Piece of cake, and cheaper than a $30 asset!

avatar image PietroBot supershwa · Aug 17, 2017 at 08:13 PM 2
Share

$$anonymous$$ake sure you also set the $$anonymous$$esh Type of the sprite to Full Rect!!!

avatar image username16 PietroBot · Feb 22, 2018 at 02:11 AM 0
Share

Good job man, ty very much! If I may know where did you find or how did you know that it needs to be Full Rect? You are a life saver!

Show more comments
avatar image Moises_Pelaez supershwa · Dec 07, 2017 at 03:30 PM 1
Share

It works with normal images with alpha but how can I make it work if I change the "Image Type" field to "Filled" and reduce the "Fill Amount"?

avatar image Elringus Moises_Pelaez · Dec 07, 2017 at 03:36 PM 1
Share

This solution only works with texture alpha and doesn't consider image fill types. If you need a complete solution for alpha-checking UI graphic check this Asset Store plugin: https://www.assetstore.unity3d.com/en/#!/content/28601

Show more comments
avatar image seyit25 supershwa · Oct 21, 2018 at 02:07 PM 0
Share

this is very easy and wonderful. thanks...

avatar image lucbloom · Jan 25 at 08:31 PM 0
Share

You can use this class to do it, without having the overhead of writable textures, for free: https://github.com/lucbloom/unity_image_with_hit_detection_on_shape

avatar image
5

Answer by robertes · Jan 25, 2017 at 09:14 PM

https://docs.unity3d.com/ScriptReference/UI.Image-alphaHitTestMinimumThreshold.html looks promising. Has anyone experimented with it?

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 Hellium · Jan 25, 2017 at 09:15 PM 1
Share

Interesting link but be careful. Having multiple images with Read/Write enabled will slow down the execution of your game.

avatar image hippogames · Feb 17, 2017 at 12:10 PM 1
Share

Yes, it works! But you have to make image readable.

 Joystick.GetComponent<Image>().alphaHitTest$$anonymous$$inimumThreshold = 0.01f;
avatar image
4

Answer by Screenhog · Mar 27, 2015 at 05:42 PM

I finally found a way to make it work for certain shapes, without resorting to custom code.

  1. Create a button.

  2. Create an image. Make it a child of the button.

  3. Offset the image so that at least part of the image is not within the quad area of the button.

  4. Press Play, and mouse over the child image. It should be a clickable surface for the button.

Now, if you create a transparent image, and use that as a child of a button, you can make a new, hidden clickable area of the button. With a little creativity, you can use three overlapping rectangular images to designate a hexagonal button area.

Obviously, this technique won't work well for certain shapes (e.g. circles, any shape with an angle less than 90 degrees). However, it certainly helps me, as I was wanting a hexagonal button.

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 Fattie · Feb 18, 2016 at 04:22 PM 0
Share

Amazing point: I guess it uses "area of any child". Wild stuff!

Note thought that (5.3) it still just uses the overall rectangle of the image - it does not know about or understand transparency.

avatar image awsapps · Jan 25 at 10:29 AM 0
Share

I was trying to do the hexagons with svgs images and turns out this is the only working solution on the web. Thanks!

avatar image lucbloom · Jan 25 at 08:41 PM 0
Share

That's a lot of steps and performance overhead to avoid dragging in a .cs file to your project.

avatar image awsapps lucbloom · Jan 26 at 11:54 AM 0
Share

Can you share a solution with a hexagonal svg and the module SVG Importer?

avatar image
3

Answer by tjPark · Oct 19, 2017 at 01:56 AM

figured it out from @robertes 's answer:

  1. in setting of your button's sprite image with alpha, check : Advanced -> Read/Write Enabled,

  2. load the Image of the button in script on Start(), give alphaHitTestMinimumThreshold value as you wish, more than 0f

codes from the unity documentation:

 public Image theButton;

 // Use this for initialization
 void Start()
 {
     theButton.alphaHitTestMinimumThreshold = 0.5f;
 }

It really works!

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
avatar image
0

Answer by RudyTheDev · Dec 14, 2014 at 04:19 PM

You can script custom areas/shapes for event interaction via ICanvasRaycastFilter.IsRaycastLocationValid. Here's a similar forum post I used for reference. Here's an implementation script I use to have an optional BoxCollider2D "mask":

 public class RaycastFilter : MonoBehaviour, ICanvasRaycastFilter
 {
     private RectTransform rectTransform;
     private new BoxCollider2D collider2D;
 
     public void Awake()
     {
         rectTransform = GetComponent<RectTransform>();
         collider2D = GetComponent<BoxCollider2D>();
     }
 
     public bool IsRaycastLocationValid(Vector2 screenPosition, Camera raycastEventCamera) //uGUI callback
     {
         // If we don't have a collider to check against, any raycast can be captured
         if (collider2D == null)
             return true;
 
         Vector2 localPoint;
         RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPosition, raycastEventCamera, out localPoint);
 
         Vector2 pivot = rectTransform.pivot - new Vector2(0.5f, 0.5f);
 
         Vector2 pivotScaled = Vector2.Scale(rectTransform.rect.size, pivot);
 
         Vector2 realPoint = localPoint + pivotScaled;
 
         Rect colliderRect = new Rect(
             collider2D.center.x - collider2D.size.x / 2,
             collider2D.center.y - collider2D.size.y / 2,
             collider2D.size.x,
             collider2D.size.y); // TODO: CACHE
 
         bool containsRect = colliderRect.Contains(realPoint);
 
         return containsRect;
     }
 }

I would imagine you can extend this to whatever needs, including actual sprite's pixels (though sliced and such would need fancier UV maths). Here's another forum post using Mask.

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

43 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

Related Questions

uGUI: Choose parent for anchors 0 Answers

3D object in uGUI 3 Answers

Content Size Fitter and Anchors / UI Scaling? 2 Answers

[Unity 4.6 uGUI] Using uGUI and unity built in sprite packer does not reduce draw calls. 0 Answers

Applying materials to the new UI elements (4.6) 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