Building my first mobile app and surprised to find out that Mouse events are not automatically converted to Touch and detected when I deploy to my Mobile device.
After some googling came across this solution (from Unity Answers)
Can any experienced mobile developers comment on whether this would be considered ‘best practice’? Or is there a better solution?
// 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");
}
}
}