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

本文关键字:Unity3d 实现 多点 移动 | 更新日期: 2023-09-27 17:56:22

我使用OnMouseDown()来处理按下,但不可能实现多点触控。

该程序包括当您点击时增加然后减少的对象。只要轻轻一按,一切正常。但是当您尝试同时单击多个对象时,它不起作用。

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

法典:

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

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

您确实不应该将鼠标事件用于触摸设备。Unity 为您提供了将第一次触摸映射到鼠标事件的便利,但仅此而已。

Unity 对触摸设备的支持:
触摸
输入.获取触摸
官方视频教程

为了在解决方案中支持多个平台(PC、平板电脑、手机等),您应该研究:
依赖于平台的编译

输入.获取触摸代码示例

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