【问题标题】:In unity3D, Click = Touch?在unity3D中,点击=触摸?
【发布时间】:2014-03-17 15:56:07
【问题描述】:

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

这是我的代码:

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

Debug.Log("Touch"); 在我点击屏幕或我的游戏对象时不显示。

【问题讨论】:

  • 您使用的是 PC 还是移动设备?在 PC 上 touchCount 将始终为 0,除非 (iirc) 您有触摸屏。
  • 我在PC上,当我点击touchCount时总是为0。这就是为什么我问点击可以被认为是Unity3D中的Touch。如果不是,我如何在PC上测试Touch事件?
  • 这取决于您是否需要多点触控。如果没有,那么 onMouseDown 可以作为测试。
  • @naXa 这可能是新功能,但我相信在 Unity 项目(Unity 4.6 和 Unity 5)中使用 OnMouseDown() 在我的 iPhone 上对我有用
  • OnMouseDown 被我的 iPhone 6+ 上的点击/触摸触发

标签: c# unity3d click touch


【解决方案1】:

简短回答:是的,可以使用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

UPD.:Unity Remote 移动应用,用于在编辑器模式下模拟触摸(适用于 Unity Editor 4 和 Unity Editor 5)。

【讨论】:

  • 非常感谢您。这是 Bart 的回答:“您是在 PC 上还是在移动设备上?在 PC 上 touchCount 将始终为 0,除非 (iirc) 您有触摸屏”。因为在 PC 上 touchCount 始终为 0,所以我只能在设备上测试您的代码?
  • @Lnitus,是的。您可以使用 OnMouse-events 和 Input.GetMouseButtonDown() 来处理:鼠标和触摸事件。但是您可以Input.touchCount(以及与触摸相关的函数)用于触摸事件。
  • 是的。我得到了它。谢谢你。
  • “虽然它对测试有好处,但我认为 Input.GetTouch() 应该在生产代码中使用”......你为什么相信这个?我正在使用 Unity 2019,单击(触摸)和右键单击(两指触摸)似乎效果很好,但如果有真正的原因,我想节省时间并直接使用触摸。
  • @GioAsencio 这是很久以前的事了......我不记得写这篇文章的确切原因。现在我刚刚检查了documentation,我看到Input.GetMouseButtonDown 被限制为3 次触摸(对应于2 个鼠标按钮)。并且通过Input.GetTouch,我们能够处理更多的触摸(通常消费设备支持多达 10 个同时触摸)。
【解决方案2】:

据我了解,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;
    }
}

【讨论】:

    【解决方案3】:

    简短的回答是否定的,有一个 unity remote android 应用程序 (remote app) 用于在编辑器模式下模拟触摸。我认为这可能会有所帮助。

    旧版 Android 应用已弃用。去related document了解更多信息,它现在也支持iOS和tvOS。

    【讨论】:

    • 他们也有一个 iOS 应用。这就是我一直在寻找的答案。
    【解决方案4】:

    晚了,但这可能对某人有帮助:)

    检测触摸和滑动(在本例中未检测到鼠标 - 也只是单点触摸)

    在 Windows 8.1(带触摸屏)和 Android 上使用 Unity 5.6 测试。

    我认为您应该单独处理鼠标单击。而且我对ios一无所知。

        bool isTouchMoved = false;
        float touchMoveThreshold = .5f;
        Vector3 posA, posB;
    
        void handleTouch ()
        {
            if (Input.touchCount > 0) {
                if (Input.GetTouch (0).phase == TouchPhase.Began) {
                    posA = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);            
                } else if (Input.GetTouch (0).phase == TouchPhase.Moved) {
                    isTouchMoved = true;
                } else {
                    posB = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
                    float dx = Mathf.Abs (posA.x - posB.x);
                    float dy = Mathf.Abs (posA.y - posB.y);
                    if (isTouchMoved && (dx > touchMoveThreshold || dy > touchMoveThreshold)) {
                        Debug.Log ("Swiped");
                        if (dx > dy) {
                            if (posA.x < posB.x) {
                                Debug.Log ("Left to Right");
                            } else {
                                Debug.Log ("Right to Left");
                            }
                        } else {
                            if (posA.y > posB.y) {
                                Debug.Log ("Top to Bottom");
                            } else {
                                Debug.Log ("Bottom to Top");
                            }
                        }
                    } else if (Input.GetTouch (0).phase == TouchPhase.Ended) {
                        Debug.Log ("Touched");
    
                        Vector2 pos2D = new Vector2 (posB.x, posB.y);
                        RaycastHit2D hit = Physics2D.Raycast (pos2D, Vector2.zero);
                        //RaycastHit2D hit = Physics2D.Raycast (Input.GetTouch (0).position, Vector2.zero);
                        if (hit.collider != null) {
                            Debug.Log ("Touched " + hit.collider.name);
                        }
                    }
                    isTouchMoved = false;
                }
            }
        }
    

    注意(1):要检测游戏对象上的触摸,您应该在该游戏对象上添加 2D 碰撞器。

    注意(2):HandleTouch()放入Update()函数中。


    更新:处理鼠标

    查看link

    if (Input.GetMouseButtonDown(0))
    {
       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
       if (hit.collider != null) {
          Debug.Log ("CLICKED " + hit.collider.name);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      相关资源
      最近更新 更多