Created
February 9, 2015 14:23
-
-
Save tsubaki/7aa2a3148496e76e39cc to your computer and use it in GitHub Desktop.
ネストしたスクロールビュー用 base http://www.programmierstube.com/unity-4-6-ui-and-nested-scrollrect/
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
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.EventSystems; | |
using System; | |
public static class FindInterface { | |
public static void DoParentEventSystemHandler<T>(this Transform self, Action<T> action) where T:IEventSystemHandler | |
{ | |
Transform parent = self.transform.parent; | |
while(parent != null) { | |
foreach(var component in parent.GetComponents<Component>()) { | |
if(component is T) | |
action((T)(IEventSystemHandler)component); | |
} | |
parent = parent.parent; | |
} | |
} | |
} |
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
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
using System; | |
using UnityEngine.EventSystems; | |
public class NestedScrollRect : ScrollRect { | |
private bool routeToParent = false; | |
public override void OnInitializePotentialDrag (PointerEventData eventData) | |
{ | |
transform.DoParentEventSystemHandler<IInitializePotentialDragHandler>((parent) => { parent.OnInitializePotentialDrag(eventData); }); | |
base.OnInitializePotentialDrag (eventData); | |
} | |
public override void OnDrag (UnityEngine.EventSystems.PointerEventData eventData) | |
{ | |
if(routeToParent) | |
transform.DoParentEventSystemHandler<IDragHandler>((parent) => { parent.OnDrag(eventData); }); | |
else | |
base.OnDrag (eventData); | |
} | |
public override void OnBeginDrag (UnityEngine.EventSystems.PointerEventData eventData) | |
{ | |
if(!horizontal && Math.Abs (eventData.delta.x) > Math.Abs (eventData.delta.y)) | |
routeToParent = true; | |
else if(!vertical && Math.Abs (eventData.delta.x) < Math.Abs (eventData.delta.y)) | |
routeToParent = true; | |
else | |
routeToParent = false; | |
if(routeToParent) | |
transform.DoParentEventSystemHandler<IBeginDragHandler>((parent) => { parent.OnBeginDrag(eventData); }); | |
else | |
base.OnBeginDrag (eventData); | |
} | |
public override void OnEndDrag (UnityEngine.EventSystems.PointerEventData eventData) | |
{ | |
if(routeToParent) | |
transform.DoParentEventSystemHandler<IEndDragHandler>((parent) => { parent.OnEndDrag(eventData); }); | |
else | |
base.OnEndDrag (eventData); | |
routeToParent = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment