Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

c# - In unity3D, Click = Touch?

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.

question from:https://stackoverflow.com/questions/22459112/in-unity3d-click-touch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 (because it is able to handle simultaneous touches).

  • 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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...