**功能解释:A【ScrollRect】竖直滑动列表下有多个Item,每个Item中有一个水平滑动列表B【ScrollRect】:
如果想要实现按压竖直滑动B时,A要对应的进行竖直滑动;
水平滑动B时,则只有B进行水平滑动;
可采用以下方法实现:**
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class AdvancedVerticalScrollRect : ScrollRect
{
public enum Direction
{
Horizontal,
Vertical,
}
[HideInInspector]
public ScrollRect ParentScrollRect;
private Direction mSelfDirection;
private Direction mCurDragDrection;
public override void OnBeginDrag(PointerEventData eventData)
{
mSelfDirection = horizontal ? Direction.Horizontal : Direction.Vertical;
mCurDragDrection = Mathf.Abs(eventData.delta.y) > Mathf.Abs(eventData.delta.x) ? Direction.Vertical : Direction.Horizontal;
if (mSelfDirection != mCurDragDrection && ParentScrollRect!= null)
{
ParentScrollRect.OnBeginDrag(eventData);
return;
}
base.OnBeginDrag(eventData);
}
public override void OnDrag(PointerEventData eventData)
{
if (mSelfDirection != mCurDragDrection && ParentScrollRect != null)
{
ParentScrollRect.OnDrag(eventData);
return;
}
base.OnDrag(eventData);
}
public override void OnEndDrag(PointerEventData eventData)
{
if (mSelfDirection != mCurDragDrection && ParentScrollRect != null)
{
ParentScrollRect.OnEndDrag(eventData);
return;
}
base.OnEndDrag(eventData);
}
public override void OnScroll(PointerEventData data)
{
if (mSelfDirection != mCurDragDrection && ParentScrollRect != null)
{
ParentScrollRect.OnScroll(data);
return;
}
base.OnScroll(data);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
[CustomEditor(typeof(AdvancedVerticalScrollRect))]
public class AdvancedVerticalScrollRectEditor : Editor
{
public override void OnInspectorGUI()
{
AdvancedVerticalScrollRect myTarget = (AdvancedVerticalScrollRect)target;
base.DrawDefaultInspector();
EditorGUILayout.Space();
EditorGUILayout.LabelField("Custom Properties", EditorStyles.boldLabel);
myTarget.ParentScrollRect = (ScrollRect)EditorGUILayout.ObjectField(
"Parent Scroll Rect",
myTarget.ParentScrollRect,
typeof(ScrollRect),
true);
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
}
}
