using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class OnZoomPanel : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler { private RectTransform canvasRect; public RectTransform transRect; private Vector2 localPos; public float minWidth; public float minHeight; //private Vector2 offSet; void Start() { canvasRect = GameObject.Find("Canvas").GetComponent(); //transRect = GetComponent(); //offSet = localPos = Vector3.zero; } public void OnBeginDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left)//不是左键按下就不能拖拽 { return; } if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, eventData.position, canvasRect.GetComponent().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;//不是左键按下就不能拖拽 Vector2 newPos; if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, eventData.position, canvasRect.GetComponent().worldCamera, out newPos)) { var dragOffset = newPos - localPos; var newX = transRect.sizeDelta.x + dragOffset.x; var newY = transRect.sizeDelta.y - dragOffset.y; if (newX < minWidth) { newX = minWidth; } if (newY < minHeight) { newY = minHeight; } transRect.sizeDelta = new Vector2(newX, newY); localPos = newPos; } } public void OnEndDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) return;//不是左键按下就不能拖拽 InputManager.isDargUI = false; } }