123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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<GraphicRaycaster>();
- // 添加EventSystem组件
- if (FindObjectOfType<EventSystem>() == null)
- {
- GameObject eventSystem = new GameObject("EventSystem");
- eventSystem.AddComponent<EventSystem>();
- eventSystem.AddComponent<StandaloneInputModule>();
- }
- scrollRect = GetComponent<ScrollRect>();
- }
- 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;
- }
- }
- }
- }
- }
|