我想在我的 2D 游戏对象上检测点击/触摸事件。
这是我的代码:
void Update()
{
if (Input.touchCount > 0)
{
Debug.Log("Touch");
}
}
Debug.Log("Touch");
在我点击屏幕或游戏对象时不显示。
最佳答案
简短回答:是的,可以使用 Input.GetMouseButtonDown()
处理触摸。
Input.GetMouseButtonDown()
、Input.mousePosition
和相关函数在触摸屏上的作用类似于点击(这有点奇怪,但值得欢迎) .如果您没有多点触控游戏,这是保持编辑器内游戏正常运行同时仍保持设备触摸输入的好方法。 (来源:Unity Community)可以使用
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)
更新:有Unity Remote用于在编辑器模式下模拟触摸的移动应用程序(适用于 Unity Editor 4 和 Unity Editor 5)。
关于c# - 在unity3D中,点击=触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22459112/