|
On my Android device, everything seem to work correctly the first time I load up my app, but if I switch to another application and back, the TouchPhase.began, and TouchPhase.ended phases simply do not fire most of the time, which makes my game unplayable. Does anyone have a suggestion for how I can fix this problem, or how I can get around it? Here's the code I've been using to track if the user touches a GUITexture:
function Update(){
for(var i : int = 0; i < Input.touchCount; i++){
var touch : Touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)){
DoSomething();
}
}
}
Any help would be MUCH appreciated! Many thanks in advance if you can solve this problem for me. =D
(comments are locked)
|
|
OK... I've come up with a second solution for this problem, this time on a more global scale. Essentially, I have provided a replacement for the Input.touches array using a static class and a single script that can be attached to any GameObject in the scene. Here are the scripts: First, the static class, you don't need an instance of this class anywhere. Just have it in your project.
Second, the script. Attach this to any object in each scene in which you want to access the alternative touches array.
oh man, you are awesome. this is an awesome solution, far better than the POS one I had hacked together. if I could give you MORE thumbs up, I absolutely would man.
6 days ago
MDReptile
(comments are locked)
|
|
In case anyone else runs into this problem, I've come up with the following code tidbit that lets me track TouchPhases in my own way, and has an easy-to-access event handler section. Still, If anyone has an actual solution to the basic problem of TouchPhases not firing correctly, I would like to see a proper answer posted here.
private var fingerLatched:boolean;
private var latchedId:int;
private var latchedPosition:Vector2;
private var button:GUITexture;
private var touchState:TouchState;
enum TouchState {
Idle = 0,
Began = 1,
Stationary = 2,
Moving = 3,
Ended = 4,
}
//custom variables
//end custom variables
function TouchHandler(){
//custom event handler
if(touchState == TouchState.Began){
DoSomething();
}
//end custom event Handler
}
function Start(){
//custom start code
//end custom start code
button = GetComponent( GUITexture );
fingerLatched = false;
latchedId = -1;
touchState = TouchState.Idle;
latchedPosition = Vector2.zero;
}
function Update(){
//custom update code
//end custom update code
touchState = TouchState.Idle;
for(var i:int = 0; i < Input.touchCount; i++){
var touch:Touch = Input.GetTouch(i);
var touchPos:Vector2 = touch.position;
if( button.HitTest(touch.position) ){
//button is being touched
if(fingerLatched){
//finger is latched
if(touch.fingerId == latchedId){
//being touched by same finger
if(touch.position == latchedPosition){
//touch is stationary
touchState = TouchState.Stationary;
}else{
//touch is moving
touchState = TouchState.Moving;
//update latched position for next frame
latchedPosition = touch.position;
}
}else{
//being touched by extra finger
}
}else{
//no finger is latched
latchFinger(touch);
touchState = TouchState.Began;
}
}
}
if(touchState == TouchState.Idle && fingerLatched){
//touch just ended (latched finger either left the button, or stopped
//touching the screen)
unLatch();
touchState = TouchState.Ended;
}
if(touchState != TouchState.Idle){
//button is being touched, or touch just ended...
TouchHandler();
}
}
function latchFinger(touch:Touch){
//latch the current finger
fingerLatched = true;
latchedId = touch.fingerId;
latchedPosition = touch.position;
}
function unLatch(){
//release the latch
fingerLatched = false;
latchedId = -1;
latchedPosition = Vector2.zero;
}
(comments are locked)
|
Unity Answers has moved to a new system, and some users may have trouble logging in.