网演高层钦州
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.
 
 
 

90 lines
3.5 KiB

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System;
using AX.InputSystem;
//处理UI拖动的脚本,该脚本需要挂在可拖动的panel(该panel需要在canvas下)下的直属子物体上,
//鼠标托这个子物体时可拖动的panel会在canvas下拖动
[RequireComponent(typeof(CreateStaticObjID))]
[RequireComponent(typeof(BaseGameObjInfo))]
public class DragPanel : MonoBehaviour, IDragHandler, IEndDragHandler,IBeginDragHandler
{
private Vector2 originalLocalPointerPosition;
private Vector3 originalPanelLocalPosition;
private RectTransform panelRectTransform;//要拖动的panel
private RectTransform parentRectTransform;//可拖动的panel的拖动范围(这里是画布Canvas)
void Start()
{
panelRectTransform = transform.parent as RectTransform;
parentRectTransform = panelRectTransform.parent as RectTransform;
}
public void OnBeginDrag(PointerEventData eventData)
{
if (eventData.button != PointerEventData.InputButton.Left) return;//不是左键按下就不能拖拽
var arg = new OnDragCmdArgs();
arg.position = eventData.position;
DragBegin(arg);
InputManager.isDargUI = true;
}
public void DragBegin(OnDragCmdArgs args)
{
RegisterUIInputEvent.RegisterUIInputHistory(gameObject, this.GetType().Name, "DragBegin", args);
originalPanelLocalPosition = panelRectTransform.localPosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, args.position, null, out originalLocalPointerPosition);
}
public void OnDrag(PointerEventData data)
{
if (data.button != PointerEventData.InputButton.Left) return;//不是左键按下就不能拖拽
var arg = new OnDragCmdArgs();
arg.position = data.position;
Drag(arg);
}
public void Drag(OnDragCmdArgs args)
{
RegisterUIInputEvent.RegisterUIInputHistory(gameObject, this.GetType().Name, "Drag", args);
if (panelRectTransform == null || parentRectTransform == null)
return;
Vector2 localPointerPosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, args.position, null, out localPointerPosition))
{
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
panelRectTransform.localPosition = originalPanelLocalPosition + offsetToOriginal;
}
ClampToWindow();
}
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;
}
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;
}
}