简体   繁体   中英

How to implement multitouch in Unity3d on mobile device?

I use OnMouseDown() to handle pressing, but it is impossible to implement multitouch.

The program includes objects that increase when you tap and then decrease. If there is one touch, everything works fine. But when you try to click on several objects at the same time it's not working.

I am trying to solve the problem, but it's not working, the objects don't scale and multi-touch doesn't work.

Code:

using UnityEngine;
using System.Collections;

public class OnTouch : MonoBehaviour {
public AudioClip crash1;
public AudioClip hat_closed;
public AudioClip hat_open;
public bool c;
public bool c1;
public bool c2;

void OnMouseDown(){

if (this.name == "clash") {
  GetComponent<AudioSource>().PlayOneShot(hat_open);
  c=true;
}
if (this.name == "clash 1") {
  GetComponent<AudioSource>().PlayOneShot(hat_closed);
  c1=true;
}

if (this.name == "clash 2") {
  GetComponent<AudioSource> ().PlayOneShot (crash1);
  c2=true;
}           

transform.localScale += new Vector3(0.05f, 0.05f, 0);

 }

void Update(){          
if (c) {transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (0.2f, 0.2f, 0), Time.deltaTime*10f);}
if (c1) {transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (0.2f, 0.2f, 0), Time.deltaTime*10f);}
if (c2) {transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (0.25f, 0.25f, 0), Time.deltaTime*10f);}
 }
}

You really should not use Mouse events for touch devices. Unity provides you the convenience of mapping the first touch to a Mouse event, but that's all.

Unity's support for Touch devices:
Touch
Input.GetTouch
Official Video Tutorial

In order to support multiple platforms (PC, Tablet, Phone, etc.) in your solution, you should look into:
Platform Dependent Compilation

Input.GetTouch code sample

public class TouchTest : MonoBehaviour 
{
    void Update () 
    {
        Touch myTouch = Input.GetTouch(0);

        Touch[] myTouches = Input.touches;
        for(int i = 0; i < Input.touchCount; i++)
        {
            //Do something with the touches
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM