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

我使用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 onm ouseDown(){

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

解决方法:

你真的不应该使用鼠标事件的触摸设备. Unity为您提供了将第一次触摸映射到鼠标事件的便利,但这就是全部.

Unity对Touch设备的支持
Touch
Input.GetTouch
Official Video Tutorial

为了在您的解决方案中支持多个平台(PC,平板电脑,手机等),您应该考虑:
Platform Dependent Compilation

Input.GetTouch代码示例

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...