Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by Eldiarc · Dec 27, 2018 at 10:30 PM · raycastbuttonclicktransparentignore

Detect click only on not transparent part of button

How do I detect click on button that don't have rectangle graphics?
Let's say my button looks like upside-down L:
alt text

I want button to activate only when pointer is over green part of image and ignore transparent part in lower right corner.

I looked up and found this guy with same problem here, so I tried using "Image.alphaHitTestMinimumThreshold" (which sounds like perfect solution for my problem) but it seems that it no longer works ('Image' does not contain a definition for it).
So... how do I do it?

buttonl.png (42.4 kB)
Comment
Add comment · Show 4
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 minecrafterlein · Dec 28, 2018 at 02:28 AM 1
Share

You might want to try getting the RaycastHit.textureCoord and use that to check the alpha channel of the objects texture.

avatar image toddisarockstar · Dec 28, 2018 at 10:12 AM 1
Share

you could simply make three smaller transparent buttons behind your image.

avatar image fafase toddisarockstar · Dec 28, 2018 at 10:27 AM 0
Share

Actually two would be enough in this case, one rectangle and one square.

avatar image Eldiarc toddisarockstar · Dec 29, 2018 at 03:54 PM 0
Share

Yeah, I think I will do that. Just two transparent child GameObjects with transparent graphics that will send info to main button object when pointer will be on them (for color change) or when player will click.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ray2yar · Dec 28, 2018 at 04:14 PM

You could get the localRect coordinate and change that to a percentage and use the percentage to get the pixel color in the texture. Just make sure the texture is read/write enabled. The code below will do it... I think... might need to play with the x and y positions a little. And definitely will need to set the pivot point to the bottom left corner (0,0).

     public RectTransform but;
     public Texture2D tex;
     
     public void OnButtonPressed()
     {
         Vector2 pos = new Vector2();
         Vector2 mouse = EventSystem.current.currentInputModule.input.mousePosition;
         RectTransformUtility.ScreenPointToLocalPointInRectangle(but,mouse , null, out pos);
 
         float px = pos.x / but.rect.width;
         float py = pos.y / but.rect.height;
 
         float tx = px * tex.width;
         float ty = py * tex.height;
 
         Color col = tex.GetPixel((int)tx, (int)ty);
     }
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 ray2yar · Dec 28, 2018 at 04:19 PM 0
Share

But, in short you can't prevent the event system from tripping on transparent pixels, you can however detect the color of the pixel and then execute a function if the color meets your criteria.

avatar image Eldiarc · Dec 29, 2018 at 03:49 PM 0
Share

It's really disappointing that Unity doesn't have function like that. One button in inspector "Ignore transparency" would solve all problems. Oh well.

avatar image
1

Answer by Chethan007 · Apr 11, 2021 at 11:52 AM

Even I was facing the same problem and figured out a solution for this:

1)For the button or the Image you are keen about uncheck RayCast Target

2)Add another button which will cover exactly that portion of the area where you want you click to be happening

3)Make it a child of the 1st Button

4)In the inspector pannel ,under color make sure that value of A=0 to make it transparent

5)Now whatever onClick operation you want to perform do it there,

Comment
Add comment · Show 1 · 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 MenimalsEntertainment · Apr 13, 2021 at 10:01 PM 0
Share

I did the same thing and it worked for me. Due to the unique shape of the button, you could even do 2 other buttons. If you're using the default code for the button where it changes color and stuff, make sure to put the target image as the one you want to change.

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

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

Related Questions

Unity 5 new UI Button Colliders moved to the left in different resolutions 1 Answer

Unity UI: neglect transparent area of button and trigger user input underneath it. 2 Answers

Unity 4.6 UI Button Click Animation? 4 Answers

How do I make a UI button to click a key? 1 Answer

gui.button down 2 Answers


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