Advertisement
Guest User

inputTouch

a guest
Mar 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class inputTouch : MonoBehaviour {
  6.  
  7.     public LayerMask touchInputMask;
  8.     private List<GameObject> touchList = new List<GameObject> ();
  9.     private GameObject[] touchesOld;
  10.     private RaycastHit hit;
  11.     // Update is called once per frame
  12.     void Update () {
  13.  
  14.  
  15.         #if UNITY_EDITOR
  16.  
  17.         #endif
  18.  
  19.         if (Input.touchCount > 0) {
  20.  
  21.  
  22.             touchesOld = new GameObject[touchList.Count];
  23.             touchList.CopyTo (touchesOld);
  24.             touchList.Clear ();
  25.  
  26.             foreach (Touch touch in Input.touches) {
  27.                 Ray ray = GetComponent<Camera> ().ScreenPointToRay (touch.position);
  28.                
  29.  
  30.                 if (Physics.Raycast (ray, out hit, touchInputMask)) {
  31.                    
  32.                     GameObject recipient = hit.transform.gameObject;
  33.                
  34.                     touchList.Add (recipient);
  35.  
  36.                     if (touch.phase == TouchPhase.Began) {
  37.                         recipient.SendMessage ("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
  38.                     }
  39.  
  40.                     if (touch.phase == TouchPhase.Ended) {
  41.                         recipient.SendMessage ("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
  42.                     }
  43.  
  44.                     if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
  45.                         recipient.SendMessage ("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
  46.                     }
  47.  
  48.                     if (touch.phase == TouchPhase.Canceled) {
  49.                         recipient.SendMessage ("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
  50.                     }
  51.                
  52.            
  53.                 }
  54.  
  55.  
  56.  
  57.             }
  58.  
  59.             foreach (GameObject g in touchesOld) {
  60.                 if (!touchList.Contains(g)){
  61.                     g.SendMessage ("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
  62.                 }
  63.  
  64.             }
  65.  
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement