unity使用IDragHandler实现拖拽

先引入命名空间 UnityEngine.EventSystems,然后引入接口IDragHandler,IBeginDragHandler

Vector3 worldPos;

    Vector3 offset;
    public void OnBeginDrag(PointerEventData eventData)
    {
         
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(), Input.mousePosition
            , eventData.enterEventCamera, out worldPos))
        {
            offset = transform.position - worldPos;
            
        }
    }


    public void OnDrag(PointerEventData eventData)
    {
        transform.position = offset + Input.mousePosition;

    }

其中EventSystems还有如下功能

  • IPointerEnterHandler - OnPointerEnter - Called when a pointer enters the object
  • IPointerExitHandler - OnPointerExit - Called when a pointer exits the object
  • IPointerDownHandler - OnPointerDown - Called when a pointer is pressed on the object
  • IPointerUpHandler - OnPointerUp - Called when a pointer is released (called on the original the pressed object)
  • IPointerClickHandler - OnPointerClick - Called when a pointer is pressed and released on the same object
  • IInitializePotentialDragHandler - OnInitializePotentialDrag - Called when a drag target is found, can be used to initialise values
  • IBeginDragHandler - OnBeginDrag - Called on the drag object when dragging is about to begin
  • IDragHandler - OnDrag - Called on the drag object when a drag is happening
  • IEndDragHandler - OnEndDrag - Called on the drag object when a drag finishes
  • IDropHandler - OnDrop - Called on the object where a drag finishes
  • IScrollHandler - OnScroll - Called when a mouse wheel scrolls
  • IUpdateSelectedHandler - OnUpdateSelected - Called on the selected object each tick
  • ISelectHandler - OnSelect - Called when the object becomes the selected object
  • IDeselectHandler - OnDeselect - Called on the selected object becomes deselected
  • IMoveHandler - OnMove - Called when a move event occurs (left, right, up, down, ect)
  • ISubmitHandler - OnSubmit - Called when the submit button is pressed
  • ICancelHandler - OnCancel - Called when the cancel button is pressed

实现Unity2D背包物品的拖拽和交换,你需要进行以下步骤: 1. 创建你的背包UI 首先,你需要创建你的背包UI。你可以使用Unity的UI工具来创建一个网格,每个格子代表一个背包物品。然后,你需要将每个物品图像作为子对象添加到每个格子上。 2. 编写拖拽脚本 接下来,你需要编写一个脚本来实现拖拽功能。该脚本应该附加到每个物品上。 在该脚本中,你需要实现以下功能: - 当用户按下鼠标左键时,开始拖动物品。 - 当用户拖动物品时,物品应该跟随鼠标移动。 - 当用户松开鼠标左键时,将物品放置在目标格子中。 - 如果目标格子中已经有物品,则将两个物品交换。 以下是一个示例脚本: ```csharp using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class DragAndDrop : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { private Transform initialParent; private Vector3 initialPosition; public void OnBeginDrag(PointerEventData eventData) { initialParent = transform.parent; initialPosition = transform.position; GetComponent<CanvasGroup>().blocksRaycasts = false; } public void OnDrag(PointerEventData eventData) { transform.position = Input.mousePosition; } public void OnEndDrag(PointerEventData eventData) { GetComponent<CanvasGroup>().blocksRaycasts = true; if (eventData.pointerEnter != null && eventData.pointerEnter.tag == "Slot") { transform.SetParent(eventData.pointerEnter.transform); if (eventData.pointerEnter.transform.childCount > 1) { transform.position = eventData.pointerEnter.transform.GetChild(1).position; eventData.pointerEnter.transform.GetChild(1).position = initialPosition; eventData.pointerEnter.transform.GetChild(1).SetParent(initialParent); } else { transform.localPosition = Vector3.zero; } } else { transform.position = initialPosition; } } } ``` 3. 添加事件触发器 最后,你需要在每个格子上添加事件触发器。当用户将物品拖动到一个格子上时,该格子将触发一个事件。你可以使用事件触发器来调用一个函数,该函数将处理物品的交换。 在事件触发器上,你需要选择“PointerDown”事件类型,并将“DragAndDrop”脚本中的“OnBeginDrag”函数添加为事件处理程序。 对于每个格子,你还需要选择“PointerUp”事件类型,并将“DragAndDrop”脚本中的“OnEndDrag”函数添加为事件处理程序。 最终,你将拥有一个可以拖拽和交换物品的背包。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值