So, say I am working on a mobile game with similar controls to Boom Beach. I have a bunch of UI elements that the user can interact with, and the user can also pan around/pinch and zoom on the map.
Basically, I need my script to capture user touch inputs that have not been processed by the UI. I’d love to receive events like OnClick, OnDrag, etc, just like the UI.
NGUI let me do this by registering an object as the ‘fallthrough’ object - this object would receive all UI events that were not handled by UI elements.
I had a system like yours for controlling the movement of my map. If the touch/drag/etc fell through it would send the messages to the script that handled Dragging the map.
Now, I have a script on the ground gameobject (with box collider) that does the following.
using UnityEngine;
using UnityEngine.EventSystems;
public class DragCamera : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
private bool WorldDragging = false;
private Vector3 DragStartPosition = new Vector3();
public void OnBeginDrag(PointerEventData eventData)
{
DragStartPosition = eventData.pointerCurrentRaycast.worldPosition;
WorldDragging = true;
}
public void OnDrag(PointerEventData eventData)
{
if (WorldDragging)
Camera.main.transform.position += DragStartPosition - eventData.pointerCurrentRaycast.worldPosition;
}
public void OnEndDrag(PointerEventData eventData)
{
if (WorldDragging)
WorldDragging = false;
}
}