#From 2015 onwards simply do this:
-
You’ll have an EventSystem in the hierarchy already.
-
Add an empty game object with a collider, bigger than the screen. Call the object “draw”. Make the layer “Draw”. In Physics Settings make that layer interact with nothing.
If the camera moves around, you my wish to add this object simply under the camera; it will move with the camera. (If you think about it, this collider represents the “actual glass of the user’s phone”.)
-
Add a physics raycaster to the camera. One click. Click the event mask, uncheck everything and check only the the “Draw” layer.
-
Have the following script on the “draw” object. You’re done.
…
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class FingerMove:MonoBehaviour,
IPointerDownHandler, IDragHandler, IPointerUpHandler
{
public void OnPointerDown (PointerEventData data)
{
Debug.Log("FINGER DOWN");
prevPointWorldSpace =
theCam.ScreenToWorldPoint( data.position );
}
public void OnDrag (PointerEventData data)
{
thisPointWorldSpace =
theCam.ScreenToWorldPoint( data.position );
realWorldTravel =
thisPointWorldSpace - prevPointWorldSpace;
_processRealWorldtravel();
prevPointWorldSpace = thisPointWorldSpace;
}
public void OnPointerUp (PointerEventData data)
{
Debug.Log("clear finger...");
}
Now if you’re just detecting finger on the “glass” - so, you don’t care about the user touching objects in the scene, you’re just interested in swipes on the glass. (Example, touch to orbit the camera.) Here’s the script - couldn’t be easier …
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class FingerMove:MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
private Vector2 prevPoint;
private Vector2 newPoint;
private Vector2 screenTravel;
public void OnPointerDown (PointerEventData data)
{
Debug.Log("FINGER DOWN");
prevPoint = data.position;
}
public void OnDrag (PointerEventData data)
{
newPoint = data.position;
screenTravel = newPoint - prevPoint;
_processSwipe();
}
public void OnPointerUp (PointerEventData data)
{
Debug.Log("FINEGR UP...");
}
private void _processSwipe()
{
// your code here
Debug.Log("screenTravel left-right.. " + screenTravel.x.ToString("f2"));
}
}
It’s that simple.
It ignores clicks on the UI. Hooray.
Much discussion and examples…
http://docs.unity3d.com/Manual/script-PhysicsRaycaster.html There is also an appropriate version for 2D
– ShinglesYour missing it. There are three new components introduced in 4.6 - GraphicsRaycaster. This is used to detect clicks on the UI - PhysicsRaycaster. This is used to detect clicks on colliders - Physics2DRaycaster. This is used to detect clicks on collider2D All if these components process through the EventSystem. The EventSystem will choose which raycaster has priority, depending on rendering order (this is still a little buggy, but it gets better each patch). This means that Physics.Raycast is no longer needed for detecting inputs over a GameObject. It also makes OnMouseXXX redundant.
– ShinglesThe video link in my answer explains the basics or click blocking. It's enough to give a competent programmer a place to start. I plan to do a complete intro for the event system, but I haven't got to it yet.
– ShinglesThe no.3 option works like a charm. But I have a question. How can I disable pointer events so they can pass thru an gameobject? In my case I have two gameobject on top of each other, and in some cases I need to catch PointerDown on the bottom one. Thank you.
– Bovid