unity3D scrollview嵌套不能滑动的问题
问题来源:
现在有这么一个需求,有一个页面,希望外面是一个水平方向滑动的scrollView A,A的子对象是一种能在垂直方向滑动的scrollview,此时,如果不做特殊处理,子对象的交互性会挡住外层scrollview的滑动性。
问题原因:
当我们点击子对象并且进行滑动时,外层的scrollview接收不到滑动事件。
解决办法:
在子对象滑动时,将拖拽事件传递给外层的scrollview,做法是给每一个子对象添加一个脚本Compoment,在这个脚本中做好拖拽事件的传递(当然,也可以在Unity的editor中,将写好的脚本拖拽到对应的scrollview对象上,只不过这样做很不灵活,本人不愿意这么操作),代码如下所示:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
//scrollViewItem 是子scrollview对象需要添加的component
public class scrollViewItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
// Start is called before the first frame update
private ScrollRect scrollview;
public void setScrollView(ScrollRect scrollview)
{
this.scrollview = scrollview;
}
//子scrollview调用OnBeginDrag时,传递OnBeginDrag到外层的scrollview,也就是这里的scrollview
public void OnBeginDrag(PointerEventData eventdata)
{
Debug.Log("aaaaaaaaaaaaaaaa");
scrollview.OnBeginDrag(eventdata);
}
public void OnDrag(PointerEventData eventdata)
{
Debug.Log("bbbbbbbbbbbbbbb");
scrollview.OnDrag(eventdata);
}
public void OnEndDrag(PointerEventData eventdata)
{
Debug.Log("ccccccc");
scrollview.OnEndDrag(eventdata);
}
}
//ListBigItem是对子scrollview对象的一个封装
public class ListBigItem
{
public ListBigItem(GameObject obj, int idx, ScrollRect _parrentRect)
{
//在子scrollview对象上添加上面的脚本
obj.AddComponent<scrollViewItem>();
//传递最外层scrollview到添加的脚本对象。
obj.GetComponent<scrollViewItem>().setScrollView(_parrentRect);
gameObject = obj;
transform = obj.transform;
curIdx = idx;
parrentRect = _parrentRect;
scrollview = gameObject.GetComponent<ScrollRect>();
init(scrollview);
}
public GameObject gameObject;
public Transform transform;
public List<ListSmallItem> smalLitemList;
ScrollRect parrentRect;
ScrollRect scrollview;
int curIdx;
public void init(ScrollRect scroll)
{
//"Prefabs/NodeDateHead" 是子scrollview对象希望包含的子节点预制体
GameObject prefabItem = Resources.Load<GameObject>("Prefabs/NodeDateHead");
int itemTotalNum = 6;
Vector3 initPos = prefabItem.transform.position;
Vector2 itemSize = prefabItem.GetComponent<RectTransform>().sizeDelta;
smalLitemList = new List<ListSmallItem>();
scroll.content.sizeDelta = new Vector2(itemSize.x, itemSize.y * itemTotalNum);
scroll.GetComponent<RectTransform>().sizeDelta = new Vector2(itemSize.x, itemSize.y * itemTotalNum);
for (int i = 0; i < itemTotalNum; i++)
{
GameObject smallItem = UnityEngine.Object.Instantiate(prefabItem, scroll.content);
smallItem.transform.localPosition = new Vector3(0, 0 - itemSize.y * i, initPos.z);
ListSmallItem item = new ListSmallItem(smallItem);
item.initData(i, curIdx);
smalLitemList.Add(item);
Debug.Log(smallItem.transform.position.y);
}
}
}
//类containt 是主类
public class containt : MonoBehaviour
{
Button preButton;
Button postButton;
ScrollRect scroll;
List<ListBigItem> bigItemList;
void Awake()
{
int weekNum = 7;
//"Prefabs/scrollViewNode" 是子scrollview预制体
GameObject item = Resources.Load<GameObject>("Prefabs/scrollViewNode");
Vector2 itemSize = item.GetComponent<RectTransform>().sizeDelta;
Vector3 initPos = item.transform.position;
bigItemList = new List<ListBigItem>();
scroll.content.sizeDelta = new Vector2(itemSize.x * weekNum, itemSize.y);
for (int i = 0; i < weekNum; i++)
{
GameObject scrollitem = Instantiate(item, scroll.content);
scrollitem.transform.localPosition = new Vector3(0 + i * itemSize.x, 0, initPos.z);
ListBigItem bigItem = new ListBigItem(scrollitem, i, scroll);
bigItemList.Add(bigItem);
}
}
}
如果有疑问,欢迎评论区一起讨论! ^_^