unity3d 简单拖拽交换对象,类似背包物品交换功能

直接挂载到需要拖拽的每个对象上就可以了

img_e372f8733d9e72383bfd978734dc09d8.png
图片.png
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class OnTestDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    private Vector3 beginPos;
    private Image image;
    void Start()
    {
        beginPos = transform.position;
        image = transform.GetComponent<Image>();
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        image.raycastTarget = false;
        beginPos = transform.position;
        transform.SetAsLastSibling();
   }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = Input.mousePosition;
        transform.localScale = new Vector3(0.8f, 0.8f, 0.8f);
        OnTestDrag drag = eventData.pointerEnter.GetComponent<OnTestDrag>();
        if (drag != null && drag.transform != transform)
        {
            transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        OnTestDrag drag = eventData.pointerEnter.GetComponent<OnTestDrag>();
        if (drag != null && drag.transform != transform)
        {
            Vector3 pos = drag.transform.position;
            drag.transform.position = beginPos;
            transform.position = pos;
            transform.localScale = Vector3.one;
        }
        else
        {
            transform.position = beginPos;
            transform.localScale = Vector3.one;

        }
        image.raycastTarget = true;
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现背包物品拖拽交换的方法: 1. 给背包中每个物品一个独特的 ID,可以用数组下标或者自定义的 ID 字段来实现。 2. 实现物品拖拽功能: - 在拖拽开始时,记录被拖拽物品的 ID 和当前物品所在背包的 ID。 - 在拖拽过程中,更新被拖拽物品的位置。 - 在拖拽结束时,判断当前物品所在背包的 ID 是否和拖拽开始时的背包 ID 相同,如果不同,则表示进行了背包之间的交换,需要将两个物品的位置进行交换。 3. 实现物品位置交换: - 获取被拖拽物品和目标物品的 ID。 - 通过 ID 获取被拖拽物品和目标物品背包中的位置。 - 交换两个物品背包中的位置,同时更新它们在 UI 上的位置。 以下是示例代码: ```csharp public class ItemSlot : MonoBehaviour, IDropHandler { public int itemId; public int slotId; private Inventory inventory; private void Start() { inventory = Inventory.instance; } public void OnDrop(PointerEventData eventData) { ItemSlot sourceSlot = eventData.pointerDrag.GetComponent<ItemSlot>(); if (sourceSlot != null && sourceSlot.slotId != slotId) { // Swap items between slots inventory.SwapItems(sourceSlot.itemId, sourceSlot.slotId, itemId, slotId); } } } public class Inventory : MonoBehaviour { public static Inventory instance; public List<Item> items = new List<Item>(); public List<int> slots = new List<int>(); private void Awake() { if (instance == null) { instance = this; } else if (instance != this) { Destroy(gameObject); } } public void SwapItems(int sourceItemId, int sourceSlotId, int targetItemId, int targetSlotId) { int sourceIndex = items.FindIndex(item => item.id == sourceItemId); int targetIndex = items.FindIndex(item => item.id == targetItemId); if (sourceIndex != -1 && targetIndex != -1) { // Swap items in inventory Item tempItem = items[sourceIndex]; items[sourceIndex] = items[targetIndex]; items[targetIndex] = tempItem; // Swap slot indices in inventory int tempSlot = slots[sourceIndex]; slots[sourceIndex] = slots[targetIndex]; slots[targetIndex] = tempSlot; // Update item slots in UI ItemSlot sourceSlot = GetItemSlot(sourceItemId); ItemSlot targetSlot = GetItemSlot(targetItemId); if (sourceSlot != null && targetSlot != null) { sourceSlot.itemId = targetItemId; targetSlot.itemId = sourceItemId; } } } private ItemSlot GetItemSlot(int itemId) { ItemSlot[] itemSlots = FindObjectsOfType<ItemSlot>(); foreach (ItemSlot itemSlot in itemSlots) { if (itemSlot.itemId == itemId) { return itemSlot; } } return null; } } ``` 在以上代码中,`ItemSlot` 是每个物品UI 界面,其中 `itemId` 表示物品的 ID,`slotId` 表示物品背包中的位置。`Inventory` 存储了所有物品和它们在背包中的位置,`SwapItems` 方法实现了交换两个物品背包中的位置,`GetItemSlot` 方法获取物品UI 中的位置。在 `ItemSlot.OnDrop` 方法中,调用 `Inventory.SwapItems` 实现物品位置交换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值