Mouse Click or Finger tap only on either game objects or UI objects

· 1 min read

When facing the situation that we want the mouse click and finger tap can happened only on either game objects or UI items. Here is how:

using UnityEngine.EventSystems;

private void MouseInput() {
  if (Input.GetButtonDown("Fire1")) {

    if (!EventSystem.current.IsPointerOverGameObject()) {
      Debug.Log(Input.mousePosition);
      Vector3 pz = Camera.main.ScreenToWorldPoint(Input.mousePosition);
      pz.z = 0;

      planetSpawner.ReleasePlanet(pz.x);
    }
  }
}

EventSystem.current.IsPointerOverGameObject() determines whether the current click point in happened on an UI item or not.