Created
February 6, 2011 23:37
-
-
Save oddlyzen/813830 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int deviationAmount = 20; | |
| public ArrayList trackers = new ArrayList(); | |
| Hashtable trackerLookup = new Hashtable(); | |
| private ArrayList ended; | |
| private TouchTracker tracker; | |
| private ArrayList movements; | |
| .... | |
| void Update () { | |
| // clean all touches (so they know if they aren't updated after we pull info) | |
| for(i=0;i<trackers.Count;i++) | |
| ((TouchTracker)trackers[i]).Clean(); | |
| // track which events vanished (without using iPhoneTouchPhase.Ended) | |
| ended = new ArrayList(); | |
| // process our touches | |
| for(i = 0;i<iPhoneInput.touches.Length;i++) | |
| { | |
| // iPhoneTouchPhase.Ended isn't very reliable (especially with the remote) | |
| // but this is how we can tell if we got single or double finger taps | |
| if(iPhoneInput.touches[i].phase == iPhoneTouchPhase.Ended && iPhoneInput.touches[i].tapCount>0) | |
| { | |
| if(iPhoneInput.touchCount == 1) | |
| { | |
| //Debug.Log("tracker.tapCount="+touch.tapCount); | |
| this.HandleSingleTap(); | |
| return; | |
| } | |
| if(iPhoneInput.touchCount == 2 && iPhoneInput.touches[i].fingerId == 1) | |
| { | |
| //Debug.Log("tracker.tapCount="+touch.tapCount); | |
| this.HandleTwoFingerTap(); | |
| return; | |
| } | |
| } | |
| else | |
| { | |
| // try to get our tracker for this finger id | |
| tracker = (TouchTracker)trackerLookup[iPhoneInput.touches[i].fingerId]; | |
| if(tracker != null) | |
| tracker.Update(iPhoneInput.touches[i]); | |
| else | |
| tracker = BeginTracking(iPhoneInput.touches[i]); | |
| } | |
| } | |
| // use an intermediate list because EndTracking removes from trackers arraylist | |
| for(i=0;i<trackers.Count;i++) | |
| { | |
| if(!((TouchTracker)trackers[i]).isDirty) | |
| ended.Add(trackers[i]); | |
| } | |
| movements = new ArrayList(); | |
| for(i=0;i<ended.Count;i++) | |
| { | |
| movements.Add(EndTracking((TouchTracker)ended[i])); | |
| } | |
| if(movements.Count == 0) | |
| return; | |
| //Debug.Log("movements.Count="+movements.Count); | |
| //single finger swipe and only do it if we have finished all trackers | |
| if(movements.Count == 1 && trackers.Count==0) | |
| { | |
| this.HandleSingleSwipe((Vector2)movements[0]); | |
| } | |
| //two finger swipe and only do it if we have finished all trackers | |
| else if(movements.Count == 2 && trackers.Count==0) | |
| { | |
| this.HandleTwoFingerSwipe((Vector2)movements[0],(Vector2)movements[1]); | |
| } | |
| } | |
| .... | |
| void HandleSingleSwipe(Vector2 movement) | |
| { | |
| if(IsSwipeRight(movement)) | |
| { | |
| //Debug.Log("Single Finger Swipe Right"); | |
| } | |
| } | |
| .... | |
| bool IsSwipeRight(Vector2 movement) | |
| { | |
| if(movement.x > deviationAmount && Mathf.Abs(movement.y) < deviationAmount) | |
| return true; | |
| return false; | |
| } | |
| TouchTracker BeginTracking(iPhoneTouch touch) | |
| { | |
| TouchTracker tracker = new TouchTracker(touch); | |
| // remember our tracker | |
| trackers.Add(tracker); | |
| trackerLookup[touch.fingerId] = tracker; | |
| return tracker; | |
| } | |
| Vector2 EndTracking(TouchTracker tracker) | |
| { | |
| Vector2 movement = tracker.End(); | |
| trackers.Remove(tracker); | |
| trackerLookup[tracker.fingerId] = null; | |
| return movement; | |
| } | |
| ..... | |
| using UnityEngine; | |
| using System.Collections; | |
| public class Swipe : MonoBehaviour { | |
| //public member vars | |
| public int swipeLength; | |
| public int swipeVariance; | |
| //private member vars | |
| private GUIText swipeText; | |
| private Vector2[] fingerTrackArray; | |
| private bool[] swipeCompleteArray; | |
| private int activeTouch = -1; | |
| //methods | |
| void Start() | |
| { | |
| //get a reference to the GUIText component | |
| swipeText = (GUIText) GetComponent(typeof(GUIText)); | |
| fingerTrackArray = new Vector2[5]; | |
| swipeCompleteArray = new bool[5]; | |
| } | |
| void Update() | |
| { | |
| //touch count is a mess at the moment so add the extra check to see if there are no more than 5 touches | |
| if(iPhoneInput.touchCount > 0 && iPhoneInput.touchCount < 6) | |
| { | |
| foreach(iPhoneTouch touch in iPhoneInput.touches) | |
| { | |
| if(touch.phase == iPhoneTouchPhase.Began) | |
| { | |
| fingerTrackArray[touch.fingerId] = touch.position; | |
| } | |
| //check if withing swipe variance | |
| if(touch.position.y > (fingerTrackArray[touch.fingerId].y + swipeVariance)) | |
| fingerTrackArray[touch.fingerId] = touch.position; | |
| if(touch.position.y < (fingerTrackArray[touch.fingerId].y - swipeVariance)) | |
| fingerTrackArray[touch.fingerId] = touch.position; | |
| //swipe right | |
| if((touch.position.x > fingerTrackArray[touch.fingerId].x + swipeLength) && !swipeCompleteArray[touch.fingerId] | |
| && activeTouch == -1) | |
| { | |
| activeTouch = touch.fingerId; | |
| //Debug.Log(touch.fingerId + " " + fingerTrackArray[touch.fingerId] + " " + touch.position); | |
| swipeCompleteArray[touch.fingerId] = true; | |
| SwipeComplete("swipe right ", touch); | |
| } | |
| //swipe left | |
| if((touch.position.x < fingerTrackArray[touch.fingerId].x - swipeLength) && !swipeCompleteArray[touch.fingerId] | |
| && activeTouch == -1) | |
| { | |
| activeTouch = touch.fingerId; | |
| //Debug.Log(touch.fingerId + " " + fingerTrackArray[touch.fingerId] + " " + touch.position); | |
| swipeCompleteArray[touch.fingerId] = true; | |
| SwipeComplete("swipe left ", touch); | |
| } | |
| //when the touch has ended we can start accepting swipes again | |
| if(touch.fingerId == activeTouch && touch.phase == iPhoneTouchPhase.Ended) | |
| { | |
| //Debug.Log("Ending " + touch.fingerId); | |
| //if more than one finger has swiped then reset the other fingers so | |
| //you do not get a double/triple etc. swipe | |
| foreach(iPhoneTouch touchReset in iPhoneInput.touches) | |
| { | |
| fingerTrackArray[touchReset.fingerId] = touchReset.position; | |
| } | |
| swipeCompleteArray[touch.fingerId] = false; | |
| activeTouch = -1; | |
| } | |
| } | |
| } | |
| } | |
| void SwipeComplete(string messageToShow, iPhoneTouch touch) | |
| { | |
| swipeText.text = messageToShow; | |
| //Debug.Log("doing something"); | |
| //Do something here | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment