Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ネストしたスクロールビュー用 base http://www.programmierstube.com/unity-4-6-ui-and-nested-scrollrect/
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;
}
}
}
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