Created
October 15, 2014 12:53
-
-
Save hecomi/2849a647df402827dad4 to your computer and use it in GitHub Desktop.
EventSystem に乗っからないで自前でイベントを管理するヤツ
This file contains hidden or 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 System.Linq; | |
using UnityEngine; | |
using System.Collections.Generic; | |
using UnityEngine.EventSystems; | |
using UnityEngine.UI; | |
public class UGUITouchFinger : MonoBehaviour | |
{ | |
public enum State | |
{ | |
Hover, Touch | |
} | |
public Camera eventCamera | |
{ | |
get { return Camera.main; } | |
} | |
public EventSystem eventSystem | |
{ | |
get { return EventSystem.current; } | |
} | |
Canvas[] canvases | |
{ | |
get { return FindObjectsOfType<Canvas>(); } | |
} | |
private Dictionary<Graphic, State> graphicsStates_ = new Dictionary<Graphic, State>(); | |
void Update() | |
{ | |
var currentGraphicStates = GetCurrentGraphicStates(); | |
DispatchEvents(currentGraphicStates); | |
graphicsStates_ = currentGraphicStates; | |
} | |
Dictionary<Graphic, State> GetCurrentGraphicStates() | |
{ | |
var fingerScreenPos = Camera.main.WorldToScreenPoint(transform.position); | |
var currentTouchingGraphicsStates = new Dictionary<Graphic, State>(); | |
foreach (var canvas in canvases.Where(c => c.enabled)) { | |
var graphics = GraphicRegistry.GetGraphicsForCanvas(canvas); | |
foreach (var graphic in graphics.Where(g => g.depth != -1)) { | |
// 範囲に収まっていないものを除く | |
if (!RectTransformUtility.RectangleContainsScreenPoint(graphic.rectTransform, fingerScreenPos, eventCamera) || | |
!graphic.Raycast(fingerScreenPos, eventCamera)) { | |
continue; | |
} | |
var distance = Vector3.Dot(graphic.transform.forward, graphic.transform.position - transform.position); | |
currentTouchingGraphicsStates.Add(graphic, (distance < 0f ? State.Touch : State.Hover)); | |
} | |
} | |
return currentTouchingGraphicsStates; | |
} | |
void DispatchEvents(Dictionary<Graphic, State> currentGraphicStates) | |
{ | |
foreach (var keyValue in currentGraphicStates) { | |
var graphic = keyValue.Key; | |
var state = keyValue.Value; | |
if (graphicsStates_.ContainsKey(graphic)) { | |
var preState = graphicsStates_[graphic]; | |
if (preState == State.Hover && state == State.Touch) { | |
ExecuteEvents.ExecuteHierarchy(graphic.gameObject, new PointerEventData(eventSystem), ExecuteEvents.pointerDownHandler); | |
Debug.Log("Touch Start"); | |
} else if (preState == State.Touch && state == State.Hover) { | |
ExecuteEvents.ExecuteHierarchy(graphic.gameObject, new PointerEventData(eventSystem), ExecuteEvents.pointerUpHandler); | |
ExecuteEvents.ExecuteHierarchy(graphic.gameObject, new PointerEventData(eventSystem), ExecuteEvents.pointerClickHandler); | |
Debug.Log("Touch End"); | |
} else { | |
// Debug.Log("-"); | |
} | |
graphicsStates_.Remove(graphic); | |
} else { | |
ExecuteEvents.ExecuteHierarchy(graphic.gameObject, new PointerEventData(eventSystem), ExecuteEvents.pointerEnterHandler); | |
if (state == State.Touch) { | |
ExecuteEvents.ExecuteHierarchy(graphic.gameObject, new PointerEventData(eventSystem), ExecuteEvents.pointerDownHandler); | |
Debug.Log("Touch Start (Direct)"); | |
} | |
} | |
} | |
foreach (var keyValue in graphicsStates_) { | |
var graphic = keyValue.Key; | |
var state = keyValue.Value; | |
if (state == State.Touch) { | |
ExecuteEvents.ExecuteHierarchy(graphic.gameObject, new PointerEventData(eventSystem), ExecuteEvents.pointerUpHandler); | |
ExecuteEvents.ExecuteHierarchy(graphic.gameObject, new PointerEventData(eventSystem), ExecuteEvents.pointerClickHandler); | |
Debug.Log("Touch End (Direct)"); | |
} | |
ExecuteEvents.ExecuteHierarchy(graphic.gameObject, new PointerEventData(eventSystem), ExecuteEvents.pointerExitHandler); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
これのやつです: https://www.youtube.com/watch?v=NY4oGnBfRO0