麦田物语学习笔记:实现拖拽物品交换数据和在地图上生成物品
基本流程
1.代码思路
(1)InventoryUI的PlayerSlots与PlayerBag里一一对应,所以想要实现交换数据实际上是,先拿到被拖拽的物体所对的Slot的序号和目标的Slot序号,然后将这两个序号对调一下
(2)物品交换的数据逻辑应该在InventoryManager里去调用,因为InventoryManager里管理了playerBag所有的数据
(3)交换数据时需要考虑库存的类型以及交换的目的,现有三个类型(slotType),有Bag,Box,Shop,依次对应的是同背包转换,跨库存数据进行转换,买卖交易;
(4)对于在地图上生成物品,首先要在SlotUI中获取拖拽结束时的世界坐标(因为Slot_Bag和已经创建好的背景不在一个层级上)
(5)新建一个ItemManager.cs,这个脚本用于管理场景中的所有物品,在切换场景的时候,保存场景当中现在有的物品,在切换回来的时候可以再次读取
(6)基于已经制作好的ItemBase的预制体,拿到这个预制体,在指定的位置进行生成,那么就需要让StotUI告诉ItemManager在哪生成,这时候就需要通过EventHandler来执行
(7)因为对事件这个知识点不是很熟,所以我会详写,在EventHandler里去实现在场景中生成物品的事件定义以及调用事件的方法,然后就可以去SlotUI里去调用了
(8)事件的详细描述:
先在事件中心EventHandler里实现对事件的定义
//在场景中生成物品的事件
//需要的参数有(ItemID,position)
public static event Action<int, Vector3> instantiateItemInScene;
再写事件的调用方法
public static void CallInstantiateItemInScene(int ID, Vector3 pos)
{InstantiateItemInScene?.Invoke(ID, pos);
}
去SlotUI中调用事件
//调用事件
EventHandler.CallInstantiateItemInScene(itemDetails.itemID,pos);
然后去ItemManager里接收数据,就需要添加注册的函数方法
private void OnEnable()
{EventHandler.InstantiateItemInScene += OnInstantiateItemInScene;
}private void OnDisable()
{EventHandler.InstantiateItemInScene -= OnInstantiateItemInScene;
}
编写方法的实现
private void OnInstantiateItemInScene(int ID, Vector3 pos)
{var item = Instantiate(itemPrefab,pos, Quaternion.identity, itemParent);item.itemID = ID;
}
2.代码实现
SlotUI中的
public void OnEndDrag(PointerEventData eventData)
{inventoryUI.dragItem.enabled = false;//Debug.Log(eventData.pointerCurrentRaycast.gameObject);//判断非空,只有非空才代表最后碰撞到的是UI物体//再判断碰撞的是否为SlotUI,不是就返回//为真就拿到双方的序号if (eventData.pointerCurrentRaycast.gameObject != null){if (eventData.pointerCurrentRaycast.gameObject.GetComponent<SlotUI>() != null){//目标点的SlotUIvar targetSlot = eventData.pointerCurrentRaycast.gameObject.GetComponent<SlotUI>();int targetIndex = targetSlot.slotIndex;//在Player自身背包范围内转换(同库存转换)if (targetSlot.slotType == SlotType.Bag && slotType == SlotType.Bag){InventoryManager.Instance.SwapItem(slotIndex, targetIndex);}//清空所有高亮 inventoryUI.UpdateSlotHightlight(-1);}}else {if (itemDetails.canDropped){//鼠标对应的世界地图坐标var pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));//调用事件EventHandler.CallInstantiateItemInScene(itemDetails.itemID, pos);}}
}
InventoryManager中的
/// <summary>
/// Player背包范围内的交换物品
/// </summary>
/// <param name="fromIndex">起始序号</param>
/// <param name="toIndex">目标数据序号</param>
public void SwapItem(int fromIndex,int toIndex)
{ //需要考虑的是,当前的格子一定是非空的,但是目标格子不一定是空InventoryItem currentItem = playerBag.itemList[fromIndex];InventoryItem targetItem = playerBag.itemList[ toIndex ];if (targetItem.itemID != 0){playerBag.itemList[fromIndex] = targetItem;playerBag.itemList[toIndex] = currentItem;}else{playerBag.itemList[fromIndex] = new InventoryItem();//这里new一个其实就是给它置空playerBag.itemList[toIndex] = currentItem;}EventHandler.CallUpdateInventoryUI(InventoryLocation.Player,playerBag.itemList);
}
EventHandler中的
//在场景中生成物品的事件
//需要的参数有(ItemID,position)
public static event Action<int, Vector3> InstantiateItemInScene;
public static void CallInstantiateItemInScene(int ID, Vector3 pos)
{InstantiateItemInScene?.Invoke(ID, pos);
}
ItemManager中的
namespace FuliFarm.Inventory
{public class ItemManager : MonoBehaviour{public Item itemPrefab;private Transform itemParent;private void OnEnable(){EventHandler.InstantiateItemInScene += OnInstantiateItemInScene;}private void OnDisable(){EventHandler.InstantiateItemInScene -= OnInstantiateItemInScene;}private void Start(){itemParent = GameObject.FindWithTag("ItemParent").transform;}private void OnInstantiateItemInScene(int ID, Vector3 pos){var item = Instantiate(itemPrefab, pos, Quaternion.identity, itemParent);item.itemID = ID;}}
}
最终效果
同库存交换
地图上拾取(就不多展示了,字面意思)
出现的问题
物品丢在地上后捡不起来,检查canPickUp没有问题,最后发现在生成itemBase的prefab时,碰撞盒的offset的y值不为零,导致碰撞盒与图片不在同一位置
相关代码是Item.cs中的这一句
将coll.offset = new Vector2(0,spriteRenderer.bounds.center.y);改为
coll.offset = new Vector2(0, spriteRenderer.transform.localPosition.y);就行了
但我实在不明白coll.offset = new Vector2(0,spriteRenderer.bounds.center.y);有什么错误,我觉得思路上是没错的