Setting a button’s alphaHitTestMinimumTreshold results into no call to Button.OnPointerDown

I want to create a button that has a custom shape in Unity.

I created a default UI>Button object and set it's Image to the following sprite: enter image description here

It doesn't show it on this website, but the circle is the only part of this image that is transparent. It's a .png sprite.

When I set this as the sprite for my button, I could click on the entire rect transform of this sprite to activate the button using my button's OnPointerDown() method and un-hide some other sprites around it:

public override void OnPointerDown(PointerEventData eventData)
{
    base.OnPointerDown(eventData);
    Debug.Log("Foo");
}

Each time any part of the sprite was clicked, transparent or not, "Foo" would print.

Now I set the button alphaHitTestMinimumThreshold to 0.5:

GetComponent<Button>().image.alphaHitTestMinimumThreshold = .5f;

And as the docs suggest, I disabled atlassing...

enter image description here

...and enabled read/write for the sprite:

enter image description here

Yet now, when I click the button in my scene.. nothing happens. No matter where I click, "Foo" doesn't print anymore.

Then I tried changing the alphaHitTestMinimumThreshold to a lot of different values. Again, the entire button was not clickable anymore. If I don't touch the alphaHitTestMinimumTreshold at all however, my "Foo" prints again.

Why does changing alphaHitTestMinimumTreshold to any value make my OnPointerDown method to never be called?



Read more here: https://stackoverflow.com/questions/64936167/setting-a-buttons-alphahittestminimumtreshold-results-into-no-call-to-button-on

Content Attribution

This content was originally published by KamielDev at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: