diff --git a/Assets/Fungus/Scripts/Components/Draggable2D.cs b/Assets/Fungus/Scripts/Components/Draggable2D.cs index 8917fd4b..ad6f227e 100644 --- a/Assets/Fungus/Scripts/Components/Draggable2D.cs +++ b/Assets/Fungus/Scripts/Components/Draggable2D.cs @@ -4,6 +4,7 @@ using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Serialization; +using System.Collections.Generic; namespace Fungus { @@ -40,6 +41,23 @@ namespace Fungus protected Vector3 newPosition; protected Vector3 delta = Vector3.zero; + #region DragCompleted handlers + protected List dragCompletedHandlers = new List(); + + public void RegisterHandler(DragCompleted handler) + { + dragCompletedHandlers.Add(handler); + } + + public void UnregisterHandler(DragCompleted handler) + { + if(dragCompletedHandlers.Contains(handler)) + { + dragCompletedHandlers.Remove(handler); + } + } + #endregion + protected virtual void LateUpdate() { // iTween will sometimes override the object position even if it should only be affecting the scale, rotation, etc. @@ -69,12 +87,6 @@ namespace Fungus EventDispatcher.Raise(new DragExited.DragExitedEvent(this, other)); } - protected virtual T[] GetHandlers() where T : EventHandler - { - // TODO: Cache these object for faster lookup - return GameObject.FindObjectsOfType(); - } - protected virtual void DoBeginDrag() { // Offset the object so that the drag is anchored to the exact point where the user clicked it @@ -113,20 +125,15 @@ namespace Fungus bool dragCompleted = false; - var handlers = GetHandlers(); - for (int i = 0; i < handlers.Length; i++) + for (int i = 0; i < dragCompletedHandlers.Count; i++) { - var handler = handlers[i]; - if (handler.DraggableObject == this) + var handler = dragCompletedHandlers[i]; + if (handler != null && handler.DraggableObject == this) { if (handler.IsOverTarget()) { - EventDispatcher.Raise(new DragCompleted.DragCompletedEvent(this)); dragCompleted = true; - if (returnOnCompleted) - { - LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); - } + EventDispatcher.Raise(new DragCompleted.DragCompletedEvent(this)); } } } @@ -140,6 +147,10 @@ namespace Fungus LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); } } + else if(returnOnCompleted) + { + LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); + } } protected virtual void DoPointerEnter() diff --git a/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs b/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs index ecdd0e2f..140b71af 100644 --- a/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs +++ b/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs @@ -38,6 +38,11 @@ namespace Fungus EventDispatcher.AddListener(OnDragCompletedEvent); EventDispatcher.AddListener(OnDragEnteredEvent); EventDispatcher.AddListener(OnDragExitedEvent); + + if(draggableObject != null) + { + draggableObject.RegisterHandler(this); + } } protected override void UnityOnDisable() @@ -45,7 +50,12 @@ namespace Fungus base.UnityOnDisable(); EventDispatcher.RemoveListener(OnDragCompletedEvent); EventDispatcher.RemoveListener(OnDragEnteredEvent); - EventDispatcher.RemoveListener(OnDragExitedEvent); + EventDispatcher.RemoveListener(OnDragExitedEvent); + + if(draggableObject != null) + { + draggableObject.UnregisterHandler(this); + } } void OnDragCompletedEvent(DragCompletedEvent evt)