Klausar Klausar - 4 months ago 79
Android Question

How do I get touch working in Unity3D?

I'm making an Android app with Unity3D and it works with click detection already, but not with touch. I need touch though for multitouch detection.

What I want: I have my player and 2 images of arrows. One arrow points right and one left. When I touch the left arrow the player should start moving left, when I touch the right arrow the player should start moving right.

But how do I detect which arrow is touched (and held)? All the code I found on Google is too old and not working anymore.

I'm working with C# scripts and it's a 2D game.

Answer

All of the above is A BIT RIDICULOUS guys :)

Klausar, what you're looking for is .........

A BUTTON (!!!)

it's that easy!

  1. click "add Canvas". (You very likely want "scale to screen size" - select that option.)

  2. click "add Button".

  3. there is no "three" .. go drinking.

In your code, have a routine like

public void UserClickedRight()
 {
 Debug.Log("click right whoo!");
 }

and same for left. Go to your right Button in the editor. Just drag the script to it and select UserClickedRight. This is explained in millions of tutorials if you have not seen it before. It's the absolutely basic mechanism and paradigm of Unity - dragging a certain script to a "slot" for it in the Editor.

Incredibly easy right?!

You DO NOT need to go to the level of touch handling to achieve this.


Now you ask, what about "when holding down on the button"?

It's incredibly easy, you just need to know a couple of things about the Unity "event" system.

Notice that in "Button", Unity "give you" a link to "OnClick" right there in the Inspector panel.

enter image description here

You completely know how to use that now, right? So, Unity put that one "right there in the panel" for convenience since you use it on most buttons.

The good news is that Button has other terrific events, you can use.

You ask, what are these other awesome events? Always simply look at the doco to find out: http://docs.unity3d.com/ScriptReference/UI.Button.html

As you can see there are terrific events like OnPointerDown and OnPointerUp. That's what you want in your case.

Again, looking at this image...

enter image description here

To repeat, Unity "give you" OnClick right there in the editor.

You may say "well, why aren't the others in there?" No mystery - it's just that OnClick is used so commonly. Say in a year or two Unity found that everyone used OnPointerDown all the time, and everyone was always yelling at them to add it in there. In that case, they'd probably add it in there for convenience. Conversely, you can imagine that they may have simply not bothered putting OnClick in there, perhaps to make that panel smaller.

The point is OnClick, OnPointerDown and so on are all events, just the same - it's just that as it happens they have "thrown in" OnClick right there on that panel, since it's popular.

Indeed, some would say this is a bit confusing: if Unity did NOT put OnClick right in there, you'd have to learn what I'm just about to explain, right? You'd have to do the following, just to use OnClick ... see what I mean?

So to use the other events is extremely simple, you just

1, make a script that references the events you want,

and,

2, you put that script on the button in question ...

it's that easy.

So. You're going to be using Unity's event system, so you'll have to do this:

  using UnityEngine.EventSystems;

Next,. You know that a script normally starts like this...

 public class FancyButton:MonoBehaviour

The "MonoBehaviour" part just means that it's a c# script which will be "driving a GamObject", it will be "part of a GameObject". But in this case we have to further alert the engine that you will be using the down and up click events. So you add this.

,IPointerDownHandler,IPointerUpHandler

So. So far we have this

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class FancyButton:MonoBehaviour,IPointerDownHandler,IPointerUpHandler
    {

Now the easy part. You just type the two routines which Unity will run for you when the things happen.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class FancyButton:MonoBehaviour,IPointerDownHandler,IPointerUpHandler
{
  public void OnPointerDown(PointerEventData data)
  {
      Debug.Log("holy! someone clicked down!")
  }

  public void OnPointerUp(PointerEventData data)
  {
      Debug.Log("whoa! someone clicked up!")
  }
}

Now all you have to do is put that script on the Button. Do so and give it a test.

So don't forget to actually put that script on the button in question. And don't forget that, in fact, you can put it on any button you like to make that button work that way. (It's common that you have a script like this, which, you put on every button in your project, or perhaps on a prefab.)

Next, click on Obi-wan to see where we're at so far!

enter image description here

Finally, it sounds like you want to do something "when the button is being held down". That's just a general programming issue - you'd have a boolean which you turn on and off as the button goes up and down. Let's do it.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class FancyButton:MonoBehaviour,IPointerDownHandler,IPointerUpHandler
    {
    [System.NonSerialized] public bool mouseIsDownRightNow;

    public void OnPointerDown(PointerEventData data)
        {
        mouseIsDownRightNow = true;
        }

    public void OnPointerUp(PointerEventData data)
        {
        mouseIsDownRightNow = false;
        }
    }

You could access that variable from another script, or whatever you want.

Add the following if you want to run a routine while the button is down:

void Update()
    {
    if (buttonIsDownRightNow) WhileButtonIsDown();
    }
private void WhileButtonIsDown()
    {
    Debug.Log("THE #$@#$ BUTTON IS DOWN!!!!!!!! WHOA!");
    }

Try that and watch the console as you hold the button down and up.

Here's an example of something like continually increasing a value while the button is down:

using UnityEngine; using System.Collections; using UnityEngine.EventSystems;

public class FancyButton:MonoBehaviour,IPointerDownHandler,IPointerUpHandler { [System.NonSerialized] public bool buttonIsDownRightNow; private int countSomething;

public void OnPointerDown(PointerEventData data)
    {
    buttonIsDownRightNow = true;
    }

public void OnPointerUp(PointerEventData data)
    {
    buttonIsDownRightNow = false;
    }

private void WhileButtonIsDown()
    {
    ++countSomething;
    }

void Update()
    {
    if (buttonIsDownRightNow) WhileButtonIsDown();

    Debug.Log("value is now " +countSomething.ToString());
    }
}

That's all there is to it. Once you really understand events in Unity, there is not much more to understand.

The only other important topics are Mecanim, shader writing, touch, coroutines, threading, PhysX, native plugins, sprites, networking, dynamic mesh, navigation, VR, AR, animation, inverse kinematics, particles, terrain, IAP, lighting, baking, shadows, MMP, character controllers...and audio. Enjoy!

Comments