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

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

问题描述

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

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

这是我的代码:

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



的debug.log(触摸); 不会当我点击屏幕或我的游戏对象显示

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

推荐答案

简答题:是的,触摸可与 Input.GetMouseButtonDown()

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


  • <处理p> Input.GetMouseButtonDown() Input.mousePosition 和相关职能的工作在触摸屏上自来水(这是种奇怪,但欢迎)。如果没有多点触控游戏,这是保持在编辑器中游戏运作良好,同时仍保持触摸输入的设备的好方法结果
    (来源:的 http://forum.unity3d.com/threads/31150-Unity -iPhone-不-A-鼠标点击一个屏幕触控结果
    虽然这是很好的测试,我相信 Input.GetTouch ()应在生产代码中使用。
  • 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: http://forum.unity3d.com/threads/31150-Unity-iPhone-Does-a-Mouse-Click-a-Screen-Touch)
    Though it is good for testing, I believe Input.GetTouch() should be used in production code.

有趣的方法是增加触控操作,以 OnMouseUp( ) / onmousedown事件()事件:

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



(来源: http://answers.unity3d.com/questions/262951/android-点击到touch.html#答案-262963

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

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