Skip to content

Commit

Permalink
Fixed to work property when under the scroll rect.
Browse files Browse the repository at this point in the history
  • Loading branch information
Haruma-K committed Nov 1, 2021
1 parent e57c044 commit f377b92
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Assets/Demo/Scenes/CarouselDemo.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Assets/FancyCarouselView/Runtime/Scripts/CarouselView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace FancyCarouselView.Runtime.Scripts
/// <typeparam name="TCell"></typeparam>
[RequireComponent(typeof(CarouselScroller))]
[RequireComponent(typeof(Image))]
[RequireComponent(typeof(ScrollEventPropagator))]
[DisallowMultipleComponent]
public abstract class CarouselView<TData, TCell> : FancyScrollView<TData, CarouselContext<TData, TCell>>,
ICarouselView<TData, TCell>, IBeginDragHandler, IDragHandler,
Expand Down
85 changes: 85 additions & 0 deletions Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs
Original file line number Diff line number Diff line change
@@ -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<IBeginDragHandler> _beginDragHandlers;
private IEnumerable<IDragHandler> _dragHandlers;
private IEnumerable<IEndDragHandler> _endDragHandlers;
private bool _isEnabled;
private Scroller _scroller;

private void Start()
{
_scroller = GetComponent<Scroller>();
var parentScrollRect = GetComponentInParent<ScrollRect>();
if (parentScrollRect != null)
{
_beginDragHandlers = parentScrollRect.GetComponents<IBeginDragHandler>();
_dragHandlers = parentScrollRect.GetComponents<IDragHandler>();
_endDragHandlers = parentScrollRect.GetComponents<IEndDragHandler>();
}
}

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);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f377b92

Please sign in to comment.