unity 背包系统(代码实现物品居中与交换)

删除UIDrag Drop Item,为格子添加Box Collider

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BeiBao : UIDragDropItem {
    public UILabel numText;//
    int count = 0;
    public void AddNum(int num)
    {
        count += num;
        numText.text = count.ToString();
    }
    protected override void OnDragDropRelease(GameObject p)
    {
        base.OnDragDropRelease(p);
        if (p.CompareTag("GeZi"))
        {
            transform.parent = p.transform;//把物品碰撞到的物体当作父级
            transform.localPosition = Vector3.zero;
        }
        else if(p.CompareTag ("WuPei"))
        {
            Transform parent = p.transform.parent;
            p.transform.parent = transform.parent;//与碰到物体交换父级  
            p.transform.localPosition = Vector3.zero;

            transform.parent = parent;
            transform.localPosition = Vector3.zero;

        }

    }

 

public class MyBeiBao : MonoBehaviour {
    public GameObject[] gezi;
    public string[] WuPinName;
    public GameObject WuPin;
    void Update()
    {
        if (Input .GetKeyDown (KeyCode.P))
        {
            PrickUp();
        }
    }
    public void PrickUp()
    {
        int index = Random.Range(0, WuPinName.Length);
        string name = WuPinName[index];
        for (int i = 0; i < gezi.Length; i++)
        {
            if (gezi[i].transform.childCount == 0)//没有物品
            {
                GameObject go = NGUITools.AddChild(gezi[i], WuPin);//生成物品
                go.GetComponent<UISprite>().spriteName = name;
                go.transform.localPosition = Vector3.zero;
                break;
            }
            else
            {
                BeiBao b = gezi[i].transform.GetChild(0).GetComponent<BeiBao>();
                if (gezi[i].transform .GetChild (0).GetComponent<UISprite>().spriteName.Equals (name))//找到物品-图片
                {
                    print("qq");
                    b.AddNum(1);
                    break;
                }
                
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值