Advertisement
Muk99

TouchButtonBehaviour

Jan 8th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. [RequireComponent (typeof (Image))]
  6.  
  7. public class TouchButtonBehaviour : MonoBehaviour {
  8.    
  9.     [HideInInspector]
  10.     public bool output;                             //Boolean output
  11.  
  12.     public bool normalize = false;                  //ButtonUp () if input position is outside graphics
  13.  
  14.     private RectTransform button;                   //Button transform
  15.     private int latchedFinger = -1;                 //Latched finger to avoid other fingers to steal button
  16.  
  17.     private float distanceX = 0;
  18.     private float distanceY = 0;
  19.  
  20.     private float canvasScale;
  21.    
  22.     void Start () {
  23.         // Cache this component at startup instead of looking up every frame
  24.         button = GetComponent <RectTransform> ();
  25.  
  26.         //Get canvas scale
  27.         canvasScale = GetComponentInParent <Canvas> ().scaleFactor;
  28.     }
  29.  
  30.     void ButtonDown () {
  31.         output = true;
  32.     }
  33.  
  34.     void ButtonUp () {
  35.         output = false;
  36.         latchedFinger = -1;
  37.     }
  38.    
  39.     void Update () {
  40.         //Graphics size /2 to calculate distance between input position and graphics position
  41.         float sizeX = button.sizeDelta.x/2 * canvasScale;
  42.         float sizeY = button.sizeDelta.y/2 * canvasScale;
  43.        
  44.         #if UNITY_EDITOR
  45.        
  46.         //Distance between button graphics and mouse position
  47.         distanceX = button.position.x - Input.mousePosition.x;
  48.         distanceY = button.position.y - Input.mousePosition.y;
  49.        
  50.         //Calculate if mouse position is inside button graphics
  51.         bool containsMousePosition = distanceX >= -sizeX &&
  52.                                      distanceX <=  sizeX &&
  53.                                      distanceY >= -sizeY &&
  54.                                      distanceY <=  sizeY ;
  55.        
  56.         //Allow click if it's inside button graphics
  57.         if (Input.GetMouseButtonDown (0) && containsMousePosition)
  58.             ButtonDown ();
  59.  
  60.         if (!containsMousePosition && output == true && normalize)
  61.             ButtonUp ();
  62.        
  63.         // Reset button if click ended
  64.         if (Input.GetMouseButtonUp (0))
  65.             ButtonUp ();
  66.  
  67.         #else
  68.  
  69.         foreach (Touch touch in Input.touches){
  70.             //Distance between button graphics snd touch position
  71.             distanceX = button.position.x - touch.position.x;
  72.             distanceY = button.position.y - touch.position.y;
  73.            
  74.             //Calculate if touch position is inside button graphics
  75.             bool containsTouchPosition = distanceX >= -sizeX &&
  76.                                          distanceX <=  sizeX &&
  77.                                          distanceY >= -sizeY &&
  78.                                          distanceY <=  sizeY ;
  79.  
  80.             //Allow click if it's inside button graphics & get latched finger id
  81.             if (containsTouchPosition && touch.phase == TouchPhase.Began){
  82.                 latchedFinger = touch.fingerId;
  83.                 ButtonDown ();
  84.             }
  85.             if (!containsTouchPosition && output == true && normalize && touch.fingerId == latchedFinger)
  86.                 ButtonUp ();
  87.            
  88.             // Reset button if touch ended
  89.             if (touch.fingerId == latchedFinger && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled))
  90.                 ButtonUp ();
  91.         }
  92.        
  93.         #endif
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement