Unity中ScrollRect拖拽子物体无法拖拽

当SrollRect下面的子物体添加了一些事件的时候,子物体的事件会阻挡ScrollRect的事件的监测,所有我们进行如下的脚本,手动调用ScrollRect事件。代码如下:


/*******************
 *  Title:CW_FrameWark
 *  Author:CW
 *  ScriptName:  DragScrollRect
 *  Des:子物体有事件拖拽等事件监听的时候,ScrollRect可以生效
 ******************/
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace CW_FrameWark
{
    public class DragScrollRect : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
    {
        [Header("要拖动的ScrollRect")]
        public ScrollRect DragScorll;
        public void OnBeginDrag(PointerEventData eventData)
        {
           if(DragScorll!=null)
            {
                DragScorll.OnBeginDrag(eventData);
            }
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (DragScorll != null)
            {
                DragScorll.OnDrag(eventData);
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (DragScorll != null)
            {
                DragScorll.OnEndDrag(eventData);
            }
        }
    }
}


原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/7700163.html