如何在移动设备上实现Unity3d中的多点触控?

时间:2015-06-15 13:56:03

标签: c# android ios mobile unity3d

我使用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);} } } 来处理按键,但是无法实现多点触控。

该程序包含点击然后减少时增加的对象。如果只有一次触摸,一切正常。但是当你试图同时点击几个对象时它就无法正常工作。

我正在尝试解决问题,但它不起作用,对象无法缩放,多点触控不起作用。

代码:

{{1}}

1 个答案:

答案 0 :(得分:2)

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
        }
    }
}