x


Detect touch on a specific GUITexture

Greetings, guys. While i'm working on my Unity3d Android project, i bumped into one thing, that I can't do properly. I'm creating an interface, and I have 2 GUITextures, each one represents a button. UP and DOWN. When I tap on one, animation plays well, but Unity does not count another GUITexture as separate button. So when I press it, it works like first button. Here is some code:


Code for button UP

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

    if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
    {
        animation["UP"].speed= 1.0;
      animation.Play("UP");
    }
    else
    {
        animation["UP"].speed= 0;
    }
}
}

Code for button DOWN

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

        if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
        {
            animation["UP"].speed= -1.0;
          animation.Play("UP");
        }
        else
        {
            animation["UP"].speed= 0;
        }
    }
    }

Im fact, I know, that i need to use "hit.test" method, but I dunno how to make it right. Thanks for your attention

more ▼

asked Feb 09 '12 at 05:45 PM

Rocotos gravatar image

Rocotos
1 1 2 3

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

3 answers: sort newest

I have another question (as I deal with previous). This script works only once, I mean when I want to play animation again and tap the button...nothing happend. What I need to add to my script?

function Update() {

 var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 var hit: RaycastHit;

 if (Input.GetMouseButtonDown(0) && Physics.Raycast(ray, hit, 100))
   {
    if (hit.collider.gameObject.tag == "newgame") 
     {
        animation.Play("newgame");
     }   

        }

    }
more ▼

answered Feb 11 '12 at 03:15 PM

Rocotos gravatar image

Rocotos
1 1 2 3

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

If you use the GUI.Button you will have more control over what buttons are being touched. The buttons will automatically react to touch inputs, which is really nice because you don't have to constantly be checking for a touch input. This is what I would do.

void OnGUI(){
     if(GUI.Button(new Rect(0,0,50,30), "Up"){
          //play animation up
     }
     if(GUI.Button(new Rect(0,40,50,30), "Down"){
          //play animation down
     }
}
more ▼

answered Feb 09 '12 at 08:02 PM

jacobschellenberg gravatar image

jacobschellenberg
61 2 2 3

It is pretty much recommended across the board to not use OnGUI in any mobile app / game developed in Unity. There is way too much overhead associated with this method. If you are going to use Unity's GUI System, the only reasonable option for mobile platforms is using GUITextures as they are considered GUIElements. GUIElements are not associated with OnGUI and thus do not exhibit the overhead of OnGUI.

Feb 09 '12 at 08:06 PM dannyskim
(comments are locked)
10|3000 characters needed characters left
function Start()
{
    var hit : GUILayer = Camera.main.GetComponent(GUILayer);
}

function Update()
{
    var hitObject : GUIElement = hit.HitTest( Input.mousePosition );

    if( hitObject != null )
    {
        switch( hitObject.name )
        {
            case "guiTexture_name1":
            // do stuff
                break;
            default:
                break;
        }
    }
}
more ▼

answered Feb 09 '12 at 06:43 PM

dannyskim gravatar image

dannyskim
3.9k 5 7 19

dannyskim, your method does not work fine. Script plays animation "UP" when i tap any part of my screen.

Feb 10 '12 at 12:26 PM Rocotos

How are you using it? You need to have at least two objects, and you need to name them uniquely. What part of the code isn't working?

Feb 10 '12 at 12:29 PM syclamoth

Well, I have 2 GUITextures called "up" and "down". I attach this script to each one, but change this line: case "guiTexture_name1" on case "up" and case "down".

case "up": animation["UP"].speed= 1.0; animation.Play("UP");

case "down": animation["UP"].speed= -1.0; animation.Play("UP");

But script "down" doesn't work at all and if I press anywhere on my touchscreen - animation UP start playing.

Feb 10 '12 at 01:12 PM Rocotos
(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:

x2490
x583
x478
x39
x17

asked: Feb 09 '12 at 05:45 PM

Seen: 3980 times

Last Updated: Feb 11 '12 at 03:16 PM