【问题标题】:How to implement multitouch in Unity3d on mobile device?如何在移动设备上的 Unity3d 中实现多点触控?
【发布时间】:2015-06-15 13:56:03
【问题描述】:

我使用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);}
 }
}

【问题讨论】:

    标签: c# android ios mobile unity3d


    【解决方案1】:

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

    Unity 对触控设备的支持:
    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
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 2020-03-14
      • 1970-01-01
      相关资源
      最近更新 更多