-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed to work property when under the scroll rect.
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/FancyCarouselView/Runtime/Scripts/ScrollEventPropagator.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.