using System; using AX.InputSystem; using UnityEngine; using UnityEngine.EventSystems; /// /// UI拖拽脚本,想拖拽谁就挂在谁身上。必须保证该UI有Raycast Target组件,并使其保持开启状态。 /// [RequireComponent(typeof(CreateStaticObjID))] [RequireComponent(typeof(BaseGameObjInfo))] public class OnDragPanel : MonoBehaviour, IDragHandler,IBeginDragHandler,IEndDragHandler { private RectTransform canvasRect; private RectTransform transRect; private Vector2 localPos; private Vector2 offSet; private 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;//不是左键按下就不能拖拽 var arg = new OnDragCmdArgs(); arg.position = eventData.position; RegisterUIInputEvent.RegisterUIInputHistory(gameObject, this.GetType().Name, "BeginDrag", arg); BeginDrag(arg); } public void BeginDrag(OnDragCmdArgs args) { if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, args.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;//不是左键按下就不能拖拽 var arg = new OnDragCmdArgs(); arg.position = eventData.position; arg.currentCursorPos = Input.mousePosition; //arg.EventCamera = eventData.pressEventCamera; ********摄像机不能记录********** RegisterUIInputEvent.RegisterUIInputHistory(gameObject, this.GetType().Name, "Drag", arg); Drag(arg); } public void Drag(OnDragCmdArgs args) { if (GameSettings.othersSettings.playState == PlayState.Playing) { CursorManager.GetInstance.SetClick(args); } if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, args.position, canvasRect.GetComponent().worldCamera, out localPos)) { transRect.localPosition = localPos - offSet; } } public void OnEndDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) return;//不是左键按下就不能拖拽 var arg = new OnDragCmdArgs(); arg.currentCursorPos = Input.mousePosition; RegisterUIInputEvent.RegisterUIInputHistory(gameObject, this.GetType().Name, "EndDrag", arg); EndDrag(arg); } public void EndDrag(CmdArgs args) { if (GameSettings.othersSettings.playState == PlayState.Playing) { CursorManager.GetInstance.SetClick(args); } InputManager.isDargUI = false; } }