|
|
@ -4,9 +4,10 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine; |
|
|
|
using System.Collections; |
|
|
|
using UnityEngine.EventSystems; |
|
|
|
using UnityEngine.Serialization; |
|
|
|
using UnityEngine.Serialization; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Fungus |
|
|
|
namespace Fungus |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
@ -17,7 +18,7 @@ namespace Fungus |
|
|
|
* The RigidBody would typically have the Is Kinematic property set to true, unless you want the object to move around using physics. |
|
|
|
* The RigidBody would typically have the Is Kinematic property set to true, unless you want the object to move around using physics. |
|
|
|
* Use in conjunction with the Drag Started, Drag Completed, Drag Cancelled, Drag Entered & Drag Exited event handlers. |
|
|
|
* Use in conjunction with the Drag Started, Drag Completed, Drag Cancelled, Drag Entered & Drag Exited event handlers. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class Draggable2D : MonoBehaviour |
|
|
|
public class Draggable2D : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler |
|
|
|
{ |
|
|
|
{ |
|
|
|
[Tooltip("Is object dragging enabled")] |
|
|
|
[Tooltip("Is object dragging enabled")] |
|
|
|
public bool dragEnabled = true; |
|
|
|
public bool dragEnabled = true; |
|
|
@ -40,38 +41,20 @@ namespace Fungus |
|
|
|
protected Vector3 newPosition; |
|
|
|
protected Vector3 newPosition; |
|
|
|
protected Vector3 delta = Vector3.zero; |
|
|
|
protected Vector3 delta = Vector3.zero; |
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseDown() |
|
|
|
protected virtual void Start() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Offset the object so that the drag is anchored to the exact point where the user clicked it |
|
|
|
// If the main camera doesn't already have a Physics2DRaycaster then add one automatically to |
|
|
|
float x = Input.mousePosition.x; |
|
|
|
// use UI raycasts for hit detection. This allows UI to block clicks on objects behind. |
|
|
|
float y = Input.mousePosition.y; |
|
|
|
if (Camera.main == null) |
|
|
|
delta = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)) - transform.position; |
|
|
|
return; |
|
|
|
delta.z = 0f; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
startingPosition = transform.position; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (DragStarted handler in GetHandlers<DragStarted>()) |
|
|
|
var raycast = Camera.main.GetComponent<Physics2DRaycaster>(); |
|
|
|
|
|
|
|
if (raycast == null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
handler.OnDragStarted(this); |
|
|
|
Camera.main.gameObject.AddComponent<Physics2DRaycaster>(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseDrag() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!dragEnabled) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float x = Input.mousePosition.x; |
|
|
|
|
|
|
|
float y = Input.mousePosition.y; |
|
|
|
|
|
|
|
float z = transform.position.z; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newPosition = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)) - delta; |
|
|
|
|
|
|
|
newPosition.z = z; |
|
|
|
|
|
|
|
updatePosition = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void LateUpdate() |
|
|
|
protected virtual void LateUpdate() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// iTween will sometimes override the object position even if it should only be affecting the scale, rotation, etc. |
|
|
|
// iTween will sometimes override the object position even if it should only be affecting the scale, rotation, etc. |
|
|
@ -83,48 +66,6 @@ namespace Fungus |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseUp() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!dragEnabled) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool dragCompleted = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DragCompleted[] handlers = GetHandlers<DragCompleted>(); |
|
|
|
|
|
|
|
foreach (DragCompleted handler in handlers) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (handler.draggableObject == this) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (handler.IsOverTarget()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
handler.OnDragCompleted(this); |
|
|
|
|
|
|
|
dragCompleted = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (returnOnCompleted) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!dragCompleted) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
foreach (DragCancelled handler in GetHandlers<DragCancelled>()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
handler.OnDragCancelled(this); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (returnOnCancelled) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnTriggerEnter2D(Collider2D other) |
|
|
|
protected virtual void OnTriggerEnter2D(Collider2D other) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!dragEnabled) |
|
|
|
if (!dragEnabled) |
|
|
@ -186,6 +127,86 @@ namespace Fungus |
|
|
|
|
|
|
|
|
|
|
|
Cursor.SetCursor(cursorTexture, Vector2.zero, CursorMode.Auto); |
|
|
|
Cursor.SetCursor(cursorTexture, Vector2.zero, CursorMode.Auto); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region IBeginDragHandler implementation |
|
|
|
|
|
|
|
public void OnBeginDrag(PointerEventData eventData) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Offset the object so that the drag is anchored to the exact point where the user clicked it |
|
|
|
|
|
|
|
float x = Input.mousePosition.x; |
|
|
|
|
|
|
|
float y = Input.mousePosition.y; |
|
|
|
|
|
|
|
delta = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)) - transform.position; |
|
|
|
|
|
|
|
delta.z = 0f; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
startingPosition = transform.position; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (DragStarted handler in GetHandlers<DragStarted>()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
handler.OnDragStarted(this); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region IDragHandler implementation |
|
|
|
|
|
|
|
public void OnDrag(PointerEventData eventData) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!dragEnabled) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float x = Input.mousePosition.x; |
|
|
|
|
|
|
|
float y = Input.mousePosition.y; |
|
|
|
|
|
|
|
float z = transform.position.z; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newPosition = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)) - delta; |
|
|
|
|
|
|
|
newPosition.z = z; |
|
|
|
|
|
|
|
updatePosition = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region IEndHandler implementation |
|
|
|
|
|
|
|
public void OnEndDrag(PointerEventData eventData) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!dragEnabled) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool dragCompleted = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DragCompleted[] handlers = GetHandlers<DragCompleted>(); |
|
|
|
|
|
|
|
foreach (DragCompleted handler in handlers) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (handler.draggableObject == this) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (handler.IsOverTarget()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
handler.OnDragCompleted(this); |
|
|
|
|
|
|
|
dragCompleted = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (returnOnCompleted) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!dragCompleted) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
foreach (DragCancelled handler in GetHandlers<DragCancelled>()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
handler.OnDragCancelled(this); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (returnOnCancelled) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|