Multi-Touch Handling on Unity iPhone

There are a lot of questions on the Unity forums about how to handle multi-touch on iPhone. We’ve done a few experiments with multi-touch–although nothing in a game yet–but here’s one solution. This had a few goals:

  • Avoid iPhoneTouchPhase, which is unreliable (particularly with the remote)
  • Abstract touch tracking logic from game logic
  • Use a minimal amount of code

Multi-Touch Example (Unity iPhone) from Matthew Wegner on Vimeo.

This solution breaks the problem up into two classes, a Tracker and a Manager.  The core of the manager uses a Hashtable to tie tracker objects to their finger IDs.  The logic looks like:

  1. Reset all trackers to “clean” state
  2. Loop through all touch events
    1. If we don’t have a tracker for this finger ID, create one (and assume touch has started)
    2. If we already have a tracker object for this finger ID, tell it to update
    3. Mark tracker as dirty
  3. Any trackers that are still marked clean are assumed to be ended

There is some logic separation here, although it isn’t as clean as it could be.  The code that’s spawning/moving cubes is placed directly into the TouchTracker class.  In production we would strive to split this up, so the grunt work of tracking touches is totally separate from any game logic.  You could have your game logic get notifications of new/ended touches, or you could subclass TouchTracker and set up functions like OnTouchStarted, OnTouchEnded, and OnTouchUpdated.  Unity presents a lot of options for situations like this.

Enjoy the example, and comment if you make any changes or improvements.  This is far from perfect, but I think it’s a good direction to take.

Download Multi-Touch Sample Project

This entry was posted in Sample Projects and tagged , . Bookmark the permalink.

5 Responses to Multi-Touch Handling on Unity iPhone

  1. Xiaoke says:

    Oh!!Cool…I am trying to have as soon as the iphone

  2. DR9885 says:

    I wish it was in C#… but ty for the build!

  3. Se7en says:

    I just wanted to say thanks for the scripts – they work so much better than the Unity built in functions especially in recognition of a touch ending or a finger sliding out of the screen area.

    I have been trying to call my external function through your system and just got it working. Instead of putting my game code inside of the TouchTracker Class I just check and store the fingerId in the external loop like so…


    for (var tracker : TouchTracker in trackers) {

    for(var touch : iPhoneTouch in iPhoneInput.touches) { // process our touches
    var tracker : TouchTracker = trackerLookup[touch.fingerId];

    if (tracker) {
    if (thrust_button_down && thrust_button_touch_ID == tracker.fingerId) {
    if (fire_button_down && fire_button_touch_ID == tracker.fingerId) {
    // Shield is a toggle
    else {
    tracker = BeginTracking(touch);
    if (!thrust_button_down) {
    if (!fire_button_down) {
    if (!shield_button_down) {

    var ended:ArrayList = new ArrayList();

    for (var tracker : TouchTracker in trackers) { if (!tracker.isDirty) { ended.Add(tracker); }}

    for (var tracker : TouchTracker in ended) {
    if (thrust_button_down && thrust_button_touch_ID == tracker.fingerId) {
    if (fire_button_down && fire_button_touch_ID == tracker.fingerId) {
    if (shield_button_down && shield_button_touch_ID == tracker.fingerId) {


    This way the TouchTracker can stay abstracted and anything new that needs to be added can accessed from outside the Class scope. Believe me I am artist not a programmer so be kind to my code, it may not be very efficient or even a good idea : ) Anyway I had to turn on and off GUITextures and other game code so I could not do it from within the Class. I thought it was going to be a huge boss fight, but its working out really well so far.

    You guys rock for sharing everything that you do, so I thought I would add something back to say thanks.

  4. Ryan R. says:

    Thanks for the code! This helped me immensely. I am working on some multitouch gesture code and intend to add to this when I get it all working.

  5. Sam Cox says:

    Thanks for the ideas guys. I didn’t use the code but took the idea and it’s working really well.

    I was getting really frustrated with TouchInputPhase not being consistent as well as not being able to check if a touch exists before grabbing it (array exceptions).

    Plus your games rock balls.

Comments are closed.