71 lines
2.2 KiB
C#
71 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class RaycastDetect : MonoBehaviour
|
|
{
|
|
public UnityAction ClickAction;
|
|
public UnityAction DefaultAction;
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
// 检测点击事件
|
|
PointerEventData pointerData = new PointerEventData(EventSystem.current)
|
|
{
|
|
position = Input.mousePosition
|
|
};
|
|
|
|
// 获取点击的UI对象
|
|
var results = new System.Collections.Generic.List<RaycastResult>();
|
|
EventSystem.current.RaycastAll(pointerData, results);
|
|
|
|
if (results.Count > 0)
|
|
{
|
|
GameObject clickedObject = results[0].gameObject;
|
|
if (clickedObject != this.gameObject)
|
|
{
|
|
//上层有其他UI
|
|
return;
|
|
}
|
|
foreach (var result in results)
|
|
{
|
|
//获取点击的按钮
|
|
if (result.gameObject.GetComponent<Button>() != null)
|
|
{
|
|
DefaultAction?.Invoke();
|
|
result.gameObject.GetComponent<Button>().OnPointerClick(pointerData);
|
|
return;
|
|
}
|
|
}
|
|
ClickAction?.Invoke();
|
|
//GameObject clickedObject = results[0].gameObject;
|
|
//if (clickedObject == mainButton.gameObject)
|
|
//{
|
|
// // 点击的是主界面按钮
|
|
// OnMainButtonClick();
|
|
//}
|
|
//else if (clickedObject.transform.IsChildOf(sidebar.transform))
|
|
//{
|
|
// // 点击的是侧边栏内部区域
|
|
// // 可以在这里处理侧边栏内部的点击事件
|
|
//}
|
|
//else
|
|
//{
|
|
// // 点击的是非交互区域
|
|
// HideSidebar();
|
|
//}
|
|
}
|
|
else
|
|
{
|
|
// 点击的是非交互区域
|
|
//HideSidebar();
|
|
}
|
|
}
|
|
}
|
|
}
|