Hi!
I thought I should share my iPhone TouchManager class with you.
I’ve included the source file of the TouchManager itself and a simple example script.
Features:
- Event handlers - Easy to use, just like OnClick, etc.
- Limit the sensitive area of the screen to mimic buttons.
- Completely abstracted in a separate super class.
If has all the features I’ve needed so far. I’ve not added anything extra since I want to make sure it all works in a production environment. Unused code tends to collect bugs…
A feature I think I will need soon is to let a script “own” a touch, making other scripts unable to respond to it. This would require a common list of “taken” touches and some means to define priorities.
Let me know if you have any ideas for this, or implement something!
185604–6573–$touch_manager_952.zip (3.25 KB)
This is great! Thanks for sharing with us crazies 
It’s nicely commented and clean, too, so people can see how it works. You should totally put this up on the wiki (http://www.unifycommunity.com/wiki/index.php?title=Main_Page)
Found a nasty bug. The timeDelta value of the Touch should not be accumulated to calculate the total time of a touch. Doh!
Updated version below.
// Limits of sensitive area. Touch must start here to be processed.
var Top : int = 0;
var Bottom : int = 480;
var Left : int = 0;
var Right : int = 320;
private var TouchActive : boolean = false;
private var Touch : iPhoneTouch;
private var InitialPosition : Vector2;
private var InitialTime : float;
function Update () {
if(TouchActive)
UpdateTouchData();
else
FindNewTouch();
}
function UpdateTouchData(){
// Try to find the touch we're tracking.
var Found : boolean = false;
for(var t : iPhoneTouch in iPhoneInput.touches)
if(t.fingerId == Touch.fingerId){
Found = true;
Touch = t;
break;
}
// Invalidate the touch when not found in the list or explicitly marked as ended.
// NOTE!!! Some people reports that the Ended/Canceled states are unreliable. Always check if the fingerId is still in use as well.
if(
!Found ||
Touch.phase == iPhoneTouchPhase.Ended ||
Touch.phase == iPhoneTouchPhase.Canceled
){
TouchActive = false;
OnTouchEnd(Touch);
}else{
// There's a valid touch to handle.
// Detect and handle movement.
if(Touch.phase == iPhoneTouchPhase.Moved)
OnTouchMove(Touch);
}
}
function FindNewTouch(){
// Find another touch to track.
for(var t : iPhoneTouch in iPhoneInput.touches)
if(TouchTrackCondition(t)){
Touch = t;
TouchActive = true;
InitialPosition = Touch.position;
InitialTime = Time.time;
OnTouchBegin(Touch);
break;
}
}
// You could override this method to get more controll over what counts as a touch.
virtual function TouchTrackCondition(Touch : iPhoneTouch){
// Track touches that *BEGIN* inside the rectangle defined by left/right/top/bottom.
return (
Touch.phase == iPhoneTouchPhase.Began
Touch.position.x >= Left
Touch.position.x <= Right
Touch.position.y >= Top
Touch.position.y <= Bottom
);
}
// Some accessors.
function TouchIsActive(){return TouchActive;}
function GetTouch(){return Touch;}
function GetTotalTime(){return Time.time - InitialTime;}
function GetTotalMovement(){return Touch.position - InitialPosition;}
// Override these custom event handlers to actually *do* stuff.
virtual function OnTouchBegin (Touch : iPhoneTouch){}
virtual function OnTouchEnd (Touch : iPhoneTouch){}
virtual function OnTouchMove (Touch : iPhoneTouch){}