namespace Plugins.CxShine.UIUtil { using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; [RequireComponent(typeof(ScrollRect))] public class ScrollRectPassThroughComp : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { private ScrollRect scrollRect; private bool isPointerDown = false; private void Awake() { // 添加GraphicRaycaster组件 gameObject.AddComponent(); // 添加EventSystem组件 if (FindObjectOfType() == null) { GameObject eventSystem = new GameObject("EventSystem"); eventSystem.AddComponent(); eventSystem.AddComponent(); } scrollRect = GetComponent(); } public void OnPointerDown(PointerEventData eventData) { isPointerDown = true; CheckPassThrough(eventData); } public void OnPointerUp(PointerEventData eventData) { isPointerDown = false; } private void CheckPassThrough(PointerEventData eventData) { if (isPointerDown) { // 计算指针位置和ScrollRect的相对位置 Vector2 localPoint; RectTransformUtility.ScreenPointToLocalPointInRectangle(scrollRect.content, eventData.position, eventData.pressEventCamera, out localPoint); // 如果指针在ScrollRect的范围内,则透传事件 if (scrollRect.content.rect.Contains(localPoint)) { eventData.pointerPressRaycast = eventData.pointerCurrentRaycast; } } } } }