在unity3D中,Click =触摸? [英] In unity3D, Click = Touch?

查看:1133
本文介绍了在unity3D中,Click =触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测我的游戏对象2D上的点击/触摸事件。



这是我的代码:

  b $ b {
if(Input.touchCount> 0)
{
Debug.Log(Touch);
}
}

Debug.Log触摸);

解决方案

:是,touch可以使用 Input.GetMouseButtonDown()




  • p> Input.GetMouseButtonDown() Input.mousePosition ,并且相关的功能在触摸屏是一种奇怪,但欢迎)。如果你没有多点触摸游戏,这是一个很好的方法来保持编辑器中的游戏运行良好,同时仍然保持设备的触摸输入。 (来源: Unity社区

    鼠标模拟触摸可以通过 Input.simulateMouseWithTouches 选项。默认情况下,此选项处于启用状态。

    虽然它适合测试,但我相信 Input.GetTouch()应该用于生产代码。 p>

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

      // OnTouchDown.cs 
    //允许OnMouseDown事件在iPhone上工作。
    //连接到主摄像头。

    使用UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class OnTouchDown:MonoBehaviour {
    void Update(){
    // iPhone中的OnMouseDown代码。引用测试。
    RaycastHit hit = new RaycastHit();
    for(int i = 0; i if(Input.GetTouch(i).phase.Equals(TouchPhase.Began)){
    / /从当前触摸坐标构造一个射线
    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配合使用)。


I want to detect click/touch event on my gameObject 2D.

And this is my code:

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

Debug.Log("Touch"); does not show when I click on screen or my gameObject.

解决方案

Short answer: yes, touch may be handled with Input.GetMouseButtonDown().

  • Input.GetMouseButtonDown(), Input.mousePosition, and associated functions work as tap on the touch screen (which is kind of odd, but welcome). If you don't have a multi-touch game, this is a good way to keep the in-editor game functioning well while still keeping touch input for devices. (source: Unity Community)
    Mouse simulation with touches can be enabled/disabled with Input.simulateMouseWithTouches option. By default this option is enabled.
    Though it is good for testing, I believe Input.GetTouch() should be used in production code.

  • Interesting approach is to add touch handling to OnMouseUp()/OnMouseDown() event:

    //  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");
                }
        }
    }
    

    (source: Unity Answers)

UPD.: There is Unity Remote mobile app for simulating touching in editor mode (works with Unity Editor 4 and Unity Editor 5).

这篇关于在unity3D中,Click =触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆