x


Android Touch Input GUITexture

I have created a game in for web which works with OnMouseDown. I have just got my Android Licence and now converting it to Android.

Is there a function similar to OnMouseDown but for OnTouch?

The code I am trying is on a GUITexture.

function OnMouseDown () {
    Application.Quit();
}

function Update(){
    if(Input.touchCount == 1){
       //Application.Quit();
    }
}

Its picking up the fact there has been a touch and it moves on but I have another GUITexture with

    function OnMouseDown(){

    Application.LoadLevel("Credits");

}

function Update(){

    if(Input.touchCount == 1){
       Application.LoadLevel("Credits");
       }

    }

But no matter where I click its always doing the credits instead of the GUITexture I am touching.

Any help would be appreciated. Thanks

more ▼

asked Jun 29 '11 at 12:59 AM

mabit gravatar image

mabit
131 12 18 21

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

GUITexture has a pretty nice function, .HitTest which you can use to test if the found touch is within the rect of the GUITexture.

Normally you would use something like

function Update()
{
 for (var touch : Touch in Input.touches)
 {
  if (guiTexture.HitTest (touch.position))
  {
    // we are now in the guitexture
    Debug.Log("Touch"); 
    exit;
  }
 }
}

This is written from head so not granted it works

more ▼

answered Jun 29 '11 at 02:26 AM

Dreamora gravatar image

Dreamora
3.2k 1 4 25

Thanks very much, while I was waiting for an answer I found this, it seems to do what you were saying.:

if(Input.touchCount > 0)
{
    var touch: Touch = Input.touches[0]; 

if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))

{ //Do Stuff Here } }

Thanks for answering. Now to replace all my OnMouseDown functions with this.

Jun 29 '11 at 02:38 AM mabit
(comments are locked)
10|3000 characters needed characters left

thats because you don't check if the touch is over the GUITexture at all. you code only checks if there is a touch.

there is no OnTouch or alike, you have to do the hittest manually or a raycast

more ▼

answered Jun 29 '11 at 02:11 AM

Dreamora gravatar image

Dreamora
3.2k 1 4 25

This is my first foray in to touch, please can you give an example, and make it idiot proof please, not the best at coding. Thanks

Jun 29 '11 at 02:13 AM mabit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2460
x579
x475

asked: Jun 29 '11 at 12:59 AM

Seen: 6717 times

Last Updated: Jun 29 '11 at 02:38 AM