Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Detect if touch is inside a specific rect transform area.

Discussion in 'UGUI & TextMesh Pro' started by Gekigengar, Oct 26, 2014.

  1. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    584
    I have 2 rectangle set up on where each is designed for a different functionality.
    How do I do this?
    Code (CSharp):
    1.     public  void OnPointerDown (PointerEventData data) {
    2.         if(Touch is inside rect 1) {
    3.              do my stuff x here
    4.         } elseif (Touch is inside rect 2) {
    5.              do my stuff y here
    6.         }
    7.     }
    Thank you!
     
    IgorAherne likes this.
  2. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  3. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    584
  4. tYn0

    tYn0

    Joined:
    Oct 21, 2014
    Posts:
    6
    What minute exactly is it? I watched whole thing but I didn't notice it
     
  5. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  6. tYn0

    tYn0

    Joined:
    Oct 21, 2014
    Posts:
    6
    Thank you. Do you know if there is a way how to use something similiar with Button?
     
  7. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  8. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    584
    Everyone looking for the same solution, its this.

    http://docs.unity3d.com/460/Documen...formUtility.RectangleContainsScreenPoint.html

    This is how I use it.

    Code (CSharp):
    1.         if (RectTransformUtility.RectangleContainsScreenPoint (
    2.             targetRectArea, data.position, data.pressEventCamera
    3.             )) {
    4.                 //targetRectArea is the rectangle transform you want your
    5.                 //input to be inside, the above will return true if it is inside.
    6.         }
    I hope this helps someone out there. ;P
     
  9. ercion

    ercion

    Joined:
    Aug 15, 2016
    Posts:
    73
    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.
     
unityunity