You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.9 KiB
52 lines
1.9 KiB
11 months ago
|
using UnityEngine;
|
||
|
using UnityEngine.EventSystems;
|
||
|
|
||
|
public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler {
|
||
|
|
||
|
private Vector2 originalLocalPointerPosition;
|
||
|
private Vector3 originalPanelLocalPosition;
|
||
|
private RectTransform panelRectTransform;
|
||
|
private RectTransform parentRectTransform;
|
||
|
|
||
|
void Start () {
|
||
|
panelRectTransform = transform.parent as RectTransform;
|
||
|
parentRectTransform = panelRectTransform.parent as RectTransform;
|
||
|
}
|
||
|
|
||
|
public void OnPointerDown (PointerEventData data) {
|
||
|
originalPanelLocalPosition = panelRectTransform.localPosition;
|
||
|
RectTransformUtility.ScreenPointToLocalPointInRectangle (parentRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition);
|
||
|
}
|
||
|
|
||
|
public void OnDrag (PointerEventData data) {
|
||
|
if (Input.GetMouseButton(0))
|
||
|
{
|
||
|
if (panelRectTransform == null || parentRectTransform == null)
|
||
|
return;
|
||
|
|
||
|
Vector2 localPointerPosition;
|
||
|
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, data.position, data.pressEventCamera, out localPointerPosition))
|
||
|
{
|
||
|
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
|
||
|
panelRectTransform.localPosition = originalPanelLocalPosition + offsetToOriginal;
|
||
|
}
|
||
|
|
||
|
ClampToWindow();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// Clamp panel to area of parent
|
||
|
void ClampToWindow () {
|
||
|
Vector3 pos = panelRectTransform.localPosition;
|
||
|
|
||
|
Vector3 minPosition = parentRectTransform.rect.min - panelRectTransform.rect.min;
|
||
|
Vector3 maxPosition = parentRectTransform.rect.max - panelRectTransform.rect.max;
|
||
|
|
||
|
pos.x = Mathf.Clamp (panelRectTransform.localPosition.x, minPosition.x, maxPosition.x);
|
||
|
pos.y = Mathf.Clamp (panelRectTransform.localPosition.y, minPosition.y, maxPosition.y);
|
||
|
|
||
|
panelRectTransform.localPosition = pos;
|
||
|
}
|
||
|
}
|