在unity3D中,Click = Touch?

我想在我的gameObject 2D上检测点击/触摸事件。

这是我的代码:

void Update() { if (Input.touchCount > 0) { Debug.Log("Touch"); } } 

Debug.Log("Touch"); 单击屏幕或我的gameObject时不显示。

简短回答:是的,可以使用Input.GetMouseButtonDown()处理触摸。

  • Input.GetMouseButtonDown()Input.mousePosition和相关函数在触摸屏上作为点击工作(这有点奇怪,但很受欢迎)。 如果您没有多点触控游戏,这是保持编辑器内游戏运行良好同时仍保持设备触摸输入的好方法。 (来源: Unity社区 )
    使用Input.simulateMouseWithTouches选项可以启用/禁用带触摸的鼠标模拟。 默认情况下,此选项已启用。
    虽然它有利于测试,但我相信Input.GetTouch()应该用在生产代码中。

  • 有趣的方法是为OnMouseUp() / OnMouseDown()事件添加触摸处理:

     // OnTouchDown.cs // Allows "OnMouseDown()" events to work on the iPhone. // Attach to the main camera. using UnityEngine; using System.Collections; using System.Collections.Generic; public class OnTouchDown : MonoBehaviour { void Update () { // Code for OnMouseDown in the iPhone. Unquote to test. RaycastHit hit = new RaycastHit(); for (int i = 0; i < Input.touchCount; ++i) if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) { // Construct a ray from the current touch coordinates Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position); if (Physics.Raycast(ray, out hit)) hit.transform.gameObject.SendMessage("OnMouseDown"); } } } 

    (来源: Unity Answers )

UPD。:Unity Remote移动应用程序用于在编辑器模式下模拟触摸(与Unity Editor 4和Unity Editor 5一起使用)。

根据我的理解,Unity播放器不允许您触发触摸事件,只触发鼠标事件。

但您可以根据鼠标事件模拟假触摸事件,如本博客文章中所述: http : //2sa-studio.blogspot.com/2015/01/simulating-touch-events-from-mouse.html

 void Update () { // Handle native touch events foreach (Touch touch in Input.touches) { HandleTouch(touch.fingerId, Camera.main.ScreenToWorldPoint(touch.position), touch.phase); } // Simulate touch events from mouse events if (Input.touchCount == 0) { if (Input.GetMouseButtonDown(0) ) { HandleTouch(10, Camera.main.ScreenToWorldPoint(Input.mousePosition), TouchPhase.Began); } if (Input.GetMouseButton(0) ) { HandleTouch(10, Camera.main.ScreenToWorldPoint(Input.mousePosition), TouchPhase.Moved); } if (Input.GetMouseButtonUp(0) ) { HandleTouch(10, Camera.main.ScreenToWorldPoint(Input.mousePosition), TouchPhase.Ended); } } } private void HandleTouch(int touchFingerId, Vector3 touchPosition, TouchPhase touchPhase) { switch (touchPhase) { case TouchPhase.Began: // TODO break; case TouchPhase.Moved: // TODO break; case TouchPhase.Ended: // TODO break; } } 

答案是否定的,有一个统一的远程 Android应用程序( Play Store )用于在编辑器模式下模拟触摸。 我认为这可能有帮助。