在列表里的元素,如果想实现OnPointerUp、OnDrag等函数,发现会和ScrollRect冲突,具体表现 就是滑动ScrollRect时,会直接调用OnPointerUp函数。除了和OnPointerUp冲突,可能嵌套ScrollRect也有冲突,没有实际测试过。
具体方案,在实现OnPointerUp的元素脚本里,同时实现OnDragBegin、OnDrag、OnDragEnd函数,在三个函数里,只负责给ScrollRect赋值。
具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class TestPointer : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler, IBeginDragHandler, IEndDragHandler { #region 负责将evenData传递给ScrollRect [SerializeField] ScrollRect scrollRect; public void OnBeginDrag(PointerEventData eventData) { scrollRect.OnBeginDrag(eventData); } public void OnDrag(PointerEventData eventData) { scrollRect.OnDrag(eventData); } public void OnEndDrag(PointerEventData eventData) { scrollRect.OnEndDrag(eventData); } #endregion public void OnPointerDown(PointerEventData eventData) { Debug.Log("OnPointerDown:" + gameObject.name); } public void OnPointerUp(PointerEventData eventData) { Debug.Log("OnPointerUp:" + gameObject.name); } } |
Git地址:https://github.com/654306663/FixScrollRectClash
- 本文固定链接: http://www.u3d8.com/?p=2281
- 转载请注明: 网虫虫 在 u3d8.com 发表过