Detect if touch is inside a specific rect transform area.

I have 2 rectangle set up on where each is designed for a different functionality.
How do I do this?

    public  void OnPointerDown (PointerEventData data) {
        if(Touch is inside rect 1) {
             do my stuff x here
        } elseif (Touch is inside rect 2) {
             do my stuff y here
        }
    }

Thank you!

1 Like

This new video explains it.

1 Like

That’s a lot of information, thanks.

What minute exactly is it? I watched whole thing but I didn’t notice it

Thank you. Do you know if there is a way how to use something similiar with Button?

1 Like

Everyone looking for the same solution, its this.

This is how I use it.

        if (RectTransformUtility.RectangleContainsScreenPoint (
            targetRectArea, data.position, data.pressEventCamera
            )) {
                //targetRectArea is the rectangle transform you want your 
                //input to be inside, the above will return true if it is inside.
        }

I hope this helps someone out there. :stuck_out_tongue_winking_eye:

17 Likes

Thank you so much for this! I wasted a lot of time trying to get RectTransform.rect.Contains to work and then found this thread.

4 Likes