最近拼ui烦躁得很,经常button被各种遮挡=。=
unity没有提供获取鼠标点中哪个ui GameObject的接口,网上也只有一堆简单的判断是不是点在ui上的判断,还以为要像以前那样,要去看源码,各种反射拿出数据。
结果源码的确看了,但是并不需要各种反射,只需要简单的几句话就能实现了。
话不多说,直接上代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEditor;
public class ClickListener : MonoBehaviour
{
List<RaycastResult> list = new List<RaycastResult>();
// Update is called once per frame
void Update()
{
/// 鼠标左键没有点击,就不执行判断逻辑
if(!Input.GetMouseButtonDown(0))
{
return;
}
///相应的GameObject对象
GameObject go = null;
///判断是否点再ui上
if (EventSystem.current.IsPointerOverGameObject())
{
go = ClickUI();
}
else
{
go = ClickScene(