c# - 在unity3D中,点击=触摸?

标签 c# unity3d click touch

我想在我的 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/

相关文章:

c# - Fiddler 测试 API Post 传递 [Frombody] 类

c# - dotnetrdf 中的 SPARQL 查询不查询导入的本体

c# - Prefab Collider2D 未被识别为 Raycast Collider

c# - UnityWebRequest 和/或 HttpWebRequest 在 Android 上使用 PUT 给出 403

android - 发现多个文件的操作系统独立路径为 'kotlin/reflect/reflect.kotlin_builtins' - Unity 项目

javascript - jQuery伪造点击事件

java - 从 java 到 c# super(ClassName.this) 参数的转换

unity3d - 在 Unity Networking - UNET 中同步复杂的游戏对象

audio - 有人单击链接后如何播放.wav

javascript - jQuery 在另一个事件中触发事件