using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class TestMouseUI : MonoBehaviour
{
GameObject currentCanvas;
// Start is called before the first frame update
void Start()
{
currentCanvas = this.gameObject;
}
// Update is called once per frame
void Update()
{
//if (EventSystem.current.IsPointerOverGameObject())
//{
// Debug.Log("当前鼠标悬浮与UI之上");
//}
if (GetOverUI(currentCanvas) != null && GetOverUI(currentCanvas).tag.Equals("3DUI"))
{
Debug.Log("标签为3DUI的UI");
}
if (GetOverUI(currentCanvas) != null && GetOverUI(currentCanvas).tag.Equals("UI"))
{
Debug.Log("标签为2DUI");
}
}
/// <summary>
/// 获取鼠标停留处UI
/// </summary>
/// <param name="canvas"></param>
/// <returns>UI物体</returns>
public static GameObject GetOverUI(GameObject canvas)
{
if (canvas.GetComponent<GraphicRaycaster>() == null) return null;
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
List<RaycastResult> results = new List<RaycastResult>();
gr.Raycast(pointerEventData, results);
if (results.Count != 0)
{
return results[0].gameObject;
}
return null;
}
}
给需要分类的UI加上相应的标签即可