40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace UIWidgets
|
|
{
|
|
public class DraggableHandle : MonoBehaviour, IDragHandler, IInitializePotentialDragHandler, IEventSystemHandler
|
|
{
|
|
private RectTransform drag;
|
|
|
|
private Canvas canvas;
|
|
|
|
private RectTransform canvasRect;
|
|
|
|
public void Drag(RectTransform newDrag)
|
|
{
|
|
drag = newDrag;
|
|
}
|
|
|
|
public virtual void OnInitializePotentialDrag(PointerEventData eventData)
|
|
{
|
|
canvasRect = Utilites.FindCanvas(base.transform) as RectTransform;
|
|
canvas = canvasRect.GetComponent<Canvas>();
|
|
}
|
|
|
|
public virtual void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (canvas == null)
|
|
{
|
|
throw new MissingComponentException(base.gameObject.name + " not in Canvas hierarchy.");
|
|
}
|
|
Vector2 localPoint;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(drag, eventData.position, eventData.pressEventCamera, out localPoint);
|
|
Vector2 localPoint2;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(drag, eventData.position - eventData.delta, eventData.pressEventCamera, out localPoint2);
|
|
Vector3 localPosition = new Vector3(drag.localPosition.x + (localPoint.x - localPoint2.x), drag.localPosition.y + (localPoint.y - localPoint2.y), drag.localPosition.z);
|
|
drag.localPosition = localPosition;
|
|
}
|
|
}
|
|
}
|