From f377b92f396097b1c4213f2eb84f1e8e6794226d Mon Sep 17 00:00:00 2001 From: Haruki Yano Date: Tue, 2 Nov 2021 00:32:59 +0900 Subject: [PATCH] Fixed to work property when under the scroll rect. --- Assets/Demo/Scenes/CarouselDemo.unity | 13 +++ .../Runtime/Scripts/CarouselView.cs | 1 + .../Runtime/Scripts/ScrollEventPropagator.cs | 85 +++++++++++++++++++ .../Scripts/ScrollEventPropagator.cs.meta | 11 +++ 4 files changed, 110 insertions(+) create mode 100644 Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs create mode 100644 Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs.meta diff --git a/Assets/Demo/Scenes/CarouselDemo.unity b/Assets/Demo/Scenes/CarouselDemo.unity index d6f06b0..33e6f2c 100644 --- a/Assets/Demo/Scenes/CarouselDemo.unity +++ b/Assets/Demo/Scenes/CarouselDemo.unity @@ -617,6 +617,7 @@ GameObject: - component: {fileID: 1539422976} - component: {fileID: 1539422974} - component: {fileID: 1539422973} + - component: {fileID: 1539422978} m_Layer: 5 m_Name: Carousel View m_TagString: Untagged @@ -733,6 +734,18 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1539422972} m_CullTransparentMesh: 0 +--- !u!114 &1539422978 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1539422972} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 85aedb7fe77344521bcb21f230abd15a, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1553209351 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/FancyCarouselView/Runtime/Scripts/CarouselView.cs b/Assets/FancyCarouselView/Runtime/Scripts/CarouselView.cs index 4b526ae..7ab3493 100644 --- a/Assets/FancyCarouselView/Runtime/Scripts/CarouselView.cs +++ b/Assets/FancyCarouselView/Runtime/Scripts/CarouselView.cs @@ -17,6 +17,7 @@ namespace FancyCarouselView.Runtime.Scripts /// [RequireComponent(typeof(CarouselScroller))] [RequireComponent(typeof(Image))] + [RequireComponent(typeof(ScrollEventPropagator))] [DisallowMultipleComponent] public abstract class CarouselView : FancyScrollView>, ICarouselView, IBeginDragHandler, IDragHandler, diff --git a/Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs b/Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs new file mode 100644 index 0000000..ad133a3 --- /dev/null +++ b/Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using FancyScrollView; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +namespace FancyCarouselView.Runtime.Scripts +{ + [RequireComponent(typeof(Scroller))] + public class ScrollEventPropagator : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler + { + private IEnumerable _beginDragHandlers; + private IEnumerable _dragHandlers; + private IEnumerable _endDragHandlers; + private bool _isEnabled; + private Scroller _scroller; + + private void Start() + { + _scroller = GetComponent(); + var parentScrollRect = GetComponentInParent(); + if (parentScrollRect != null) + { + _beginDragHandlers = parentScrollRect.GetComponents(); + _dragHandlers = parentScrollRect.GetComponents(); + _endDragHandlers = parentScrollRect.GetComponents(); + } + } + + void IBeginDragHandler.OnBeginDrag(PointerEventData eventData) + { + if (_scroller.ScrollDirection == ScrollDirection.Vertical + && Math.Abs(eventData.delta.x) > Math.Abs(eventData.delta.y)) + { + _isEnabled = true; + } + else if (_scroller.ScrollDirection == ScrollDirection.Horizontal + && Math.Abs(eventData.delta.x) < Math.Abs(eventData.delta.y)) + { + _isEnabled = true; + } + else + { + _isEnabled = false; + } + + if (!_isEnabled || _beginDragHandlers == null) + { + return; + } + + foreach (var handler in _beginDragHandlers) + { + handler.OnBeginDrag(eventData); + } + } + + void IDragHandler.OnDrag(PointerEventData eventData) + { + if (!_isEnabled || _dragHandlers == null) + { + return; + } + + foreach (var handler in _dragHandlers) + { + handler.OnDrag(eventData); + } + } + + void IEndDragHandler.OnEndDrag(PointerEventData eventData) + { + if (!_isEnabled || _endDragHandlers == null) + { + return; + } + + foreach (var handler in _endDragHandlers) + { + handler.OnEndDrag(eventData); + } + } + } +} \ No newline at end of file diff --git a/Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs.meta b/Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs.meta new file mode 100644 index 0000000..47c4241 --- /dev/null +++ b/Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 85aedb7fe77344521bcb21f230abd15a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: