在做项目时遇到需要最上层ui和下一层ui同时收到点击事件的需求,于是写了一个小工具,只要把这个脚本挂到最上层ui上就好了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
//Encoding:UTF-8
public class UIPointerEventPenetrate : MonoBehaviour, IPointerClickHandler
{
List<RaycastResult> results = new List<RaycastResult>();
public void OnPointerClick(PointerEventData eventData)
{
Psss(eventData, ExecuteEvents.pointerClickHandler);
}
public void Psss<T>(PointerEventData data, ExecuteEvents.EventFunction<T> function) where T : IEventSystemHandler
{
results.Clear();
EventSystem.current.RaycastAll(data, results);
GameObject current = data.pointerCurrentRaycast.gameObject;
for (int i = 0; i < results.Count; i++)
{
if (current != results[i].gameObject)
{
if (ExecuteEvents.Execute(results[i].gameObject, data, function))
{
break;
}
}
}
}
}
前人栽树后人乘凉,乘凉之后也要记得栽树哦。