using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class isonui : MonoBehaviour { // Update is called once per frame void Update() { //移动端 if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { if (IsPointerOverGameObject(Input.GetTouch(0).position)) { Debug.Log("点击到UI"); } } } //其它平台 else { if (Input.GetMouseButtonDown(0)) { if (IsPointerOverGameObject(Input.mousePosition)) { Debug.Log("点击到UI"); } } } } /// /// 检测是否点击UI /// /// /// private bool IsPointerOverGameObject(Vector2 mousePosition) { //创建一个点击事件 PointerEventData eventData = new PointerEventData(EventSystem.current); eventData.position = mousePosition; List raycastResults = new List(); //向点击位置发射一条射线,检测是否点击UI EventSystem.current.RaycastAll(eventData, raycastResults); if (raycastResults.Count > 0) { return true; } else { return false; } } }