示例:点击UI时,被遮挡的UI也响应
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ClollideUI : MonoBehaviour, IPointerClickHandler
{
private bool _toBlue;
private void ChangeColor()
{
_toBlue = !_toBlue;
GetComponent<Image>().material.SetColor("_Color", _toBlue ? Color.blue : Color.white);
}
public void OnPointerClick(PointerEventData eventData)
{
ChangeColor();
ExecuteAll(eventData, ExecuteEvents.pointerClickHandler);
}
private void ExecuteAll<T>(PointerEventData eventData, ExecuteEvents.EventFunction<T> functor) where T : IEventSystemHandler
{
// 射线检测全部响应物体
var results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, results);
// 转发事件给其他响应物体
foreach (var result in results.Where(result => result.gameObject != gameObject))
{
ExecuteEvents.Execute(result.gameObject, eventData, functor);
}
}
}