- Home /
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:
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?
You might want to try getting the RaycastHit.textureCoord and use that to check the alpha channel of the objects texture.
you could simply make three smaller transparent buttons behind your image.
Actually two would be enough in this case, one rectangle and one square.
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.
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);
}
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.
It's really disappointing that Unity doesn't have function like that. One button in inspector "Ignore transparency" would solve all problems. Oh well.
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,
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
Follow this Question
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
RepeatButton AND Button at the same time 1 Answer
Is there a way to click on a render texture to select soemthing within the view? 4 Answers
How to prioritise detection of Button click in RTS building placement using raycast 1 Answer