Unity irregular button

     Usually the buttons we create are square, but sometimes the project requirements let us click an irregular button.

I checked a lot on the Internet to rewrite the Iamge component on Button, basically adding Polygon Collider2D to complete it. But what I am doing is a VR project and what I want to do is 3DUI, so this involves coordinate transformation issues. Might be a little more troublesome.

Finally found a very simple way to record it here

1. First of all, let's open the read and write permissions of our pictures

 

2. Get the Image on the Button in the script and let

alphaHitTestMinimumThreshold is equal to 0.1f

alphaHitTestMinimumThreshold will judge the click according to the alpha value

using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour
{
    private Image image;

    private void Start()
    {
        image = transform.GetComponent<Image>();
        image.alphaHitTestMinimumThreshold = 0.1f;
    }
}

That's it. Is not it simple! !

Guess you like

Origin blog.csdn.net/m0_55540902/article/details/126319631