And for mobile platforms I am using this code, but it doesn't work. The system doesn't check that user pressed on UI image. public class TouchController : MonoBehaviour { Canvas BonusesAndPotionsCanvas; GameObject BonusesAndPotions; void Start () { BonusesAndPotions = GameObject.FindGameObjectWithTag ("bonuses_and_potions"); BonusesAndPotionsCanvas = BonusesAndPotions.GetComponent (); }
public class TouchController : MonoBehaviour {
Canvas BonusesAndPotionsCanvas;
GameObject BonusesAndPotions;
void UpdateStart ()
{
BonusesAndPotions = GameObject.FindGameObjectWithTag ("bonuses_and_potions");
BonusesAndPotionsCanvas = BonusesAndPotions.GetComponent<Canvas> ();
}
void Update()
{
EventSystem eventSystem = EventSystem.current;
if (eventSystem.currentSelectedGameObject != null)
return;
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint( Input.GetTouch(i).position );
if (IsPointerOverUIObject(BonusesAndPotionsCanvas, worldPoint))
return;
}
}
}
private bool IsPointerOverUIObject(Canvas canvas, Vector2 screenPosition)
{
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = screenPosition;
GraphicRaycaster uiRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>();
List<RaycastResult> results = new List<RaycastResult>();
uiRaycaster.Raycast(eventDataCurrentPosition, results);
return results.Count > 0;
}
}