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.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

[Solved][RayCast]MultiTouch Input: Convert local RectTransform.rect to screen position.

Discussion in 'UGUI & TextMesh Pro' started by IsGreen, Apr 11, 2015.

  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    I think, UI and GUI system don't support MultiTouch Input.

    I follow MultiTouch Input Tutorial(code):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TouchTest : MonoBehaviour
    5. {
    6.     void Update ()
    7.     {
    8.         Touch[] myTouches = Input.touches;
    9.         for(int i = 0; i < Input.touchCount; i++)
    10.         {
    11.             //Do something with the touches
    12.  
    13.         }
    14.     }
    15. }
    I would like to use Rect.Contains(myTouches(i).position), but i need convert local rect from RectTransform component to screen position.
     
    Last edited: Apr 11, 2015
  2. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Finally, I have substituted a uButton by uImage, and change the color(appearance) manually.

    To enable MultiTouch Input, a Canvas has a GraphicRaycaster attached by default, which allows EventSystem.current.RaycastAll to hit uGUI elements that are Graphics (Image, Text, etc).

    Raycasting in new GUI
    (Unity 4.6) How to raycast against uGUI objects from an arbitrary screen/canvas position

    Example Code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using System.Collections.Generic;
    5.  
    6. public class uImageRaycast : MonoBehaviour {
    7.  
    8.     public GameObject uImage;
    9.  
    10.     Image image;
    11.     Color colorOn,colorOff;
    12.  
    13.     void Start(){
    14.  
    15.         this.image = this.uImage.GetComponent<Image>();
    16.         this.colorOff = this.image.color;
    17.         this.colorOn = new Color(this.colorOff.r,this.colorOff.g,this.colorOff.b,this.colorOff.a*0.5f);
    18.  
    19.     }
    20.  
    21.     void Update(){
    22.  
    23.         this.image.color = this.colorOff;
    24.  
    25.         PointerEventData pointer = new PointerEventData(EventSystem.current);
    26.         List<RaycastResult> raycastResult = new List<RaycastResult>();
    27.  
    28.         foreach (Touch touch in Input.touches){
    29.  
    30.             pointer.position = touch.position;
    31.             EventSystem.current.RaycastAll(pointer,raycastResult);
    32.  
    33.             foreach(RaycastResult result in raycastResult){
    34.  
    35.                 if(result.gameObject == this.uImage){
    36.  
    37.                     this.image.color = this.colorOn;
    38.  
    39.                     //Do Stuff
    40.  
    41.                 }              
    42.  
    43.             }
    44.  
    45.             raycastResult.Clear();      
    46.  
    47.         }
    48.  
    49.     }
    50.  
    51. }
    52.  
     
    singapi likes this.