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.
69 lines
2.7 KiB
69 lines
2.7 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.EventSystems; |
|
|
|
public class DragVideoPanel : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler |
|
{ |
|
private RectTransform canvasRect; |
|
public RectTransform transRect; |
|
private Vector2 localPos; |
|
private Vector2 offSet; |
|
public bool limit; |
|
private Vector2 anchoredPosition; |
|
|
|
private void Start() |
|
{ |
|
canvasRect = GameObject.Find("Canvas").GetComponent<RectTransform>(); |
|
offSet = localPos = Vector3.zero; |
|
anchoredPosition = transRect.anchoredPosition; |
|
} |
|
|
|
public void OnBeginDrag(PointerEventData eventData) |
|
{ |
|
if (eventData.button != PointerEventData.InputButton.Left) return;//不是左键按下就不能拖拽 |
|
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, eventData.position, canvasRect.GetComponent<Canvas>().worldCamera, out localPos)) |
|
{ |
|
offSet = localPos - new Vector2(transRect.localPosition.x, transRect.localPosition.y); |
|
InputManager.isDargUI = true; |
|
//transform.SetAsLastSibling(); |
|
} |
|
} |
|
|
|
public void OnDrag(PointerEventData eventData) |
|
{ |
|
if (eventData.button != PointerEventData.InputButton.Left) return;//不是左键按下就不能拖拽 |
|
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, eventData.position, canvasRect.GetComponent<Canvas>().worldCamera, out localPos)) |
|
{ |
|
|
|
transRect.localPosition = localPos - offSet; |
|
} |
|
} |
|
|
|
public void OnEndDrag(PointerEventData eventData) |
|
{ |
|
if (eventData.button != PointerEventData.InputButton.Left) return;//不是左键按下就不能拖拽 |
|
InputManager.isDargUI = false; |
|
if (limit) |
|
{ |
|
//if (OutLimit()) |
|
//{ |
|
//transRect.anchoredPosition = anchoredPosition; |
|
// } |
|
OutLimit(); |
|
} |
|
} |
|
|
|
void OutLimit() |
|
{ |
|
Vector3 pos = transRect.localPosition; |
|
|
|
Vector3 minPosition = transRect.parent.GetComponent<RectTransform>().rect.min - transRect.rect.min; |
|
Vector3 maxPosition = transRect.parent.GetComponent<RectTransform>().rect.max - transRect.rect.max; |
|
|
|
pos.x = Mathf.Clamp(transRect.localPosition.x, minPosition.x - transRect.rect.width + 50f, maxPosition.x + transRect.rect.width - 50f); |
|
pos.y = Mathf.Clamp(transRect.localPosition.y, minPosition.y - transRect.rect.height + 100f, maxPosition.y + transRect.rect.height - 100f); |
|
|
|
transRect.localPosition = pos; |
|
} |
|
}
|
|
|