当前位置: 首页 > news >正文

麦田物语学习笔记:实现拖拽物品交换数据和在地图上生成物品

基本流程

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);有什么错误,我觉得思路上是没错的

http://www.lryc.cn/news/519807.html

相关文章:

  • 一些计算机零碎知识随写(25年1月)-1
  • Qt学习笔记第81到90讲
  • Centos9 + Docker 安装 MySQL8.4.0 + 定时备份数据库到本地
  • 网络原理一>UDP协议详解
  • MySQL的小问题
  • Mac——Docker desktop安装与使用教程
  • FastApi Swagger 序列化问题
  • 《机器学习》——sklearn库中CountVectorizer方法(词频矩阵)
  • UML系列之Rational Rose笔记三:活动图(泳道图)
  • Java面向对象面经总结
  • 红队工具使用全解析:揭开网络安全神秘面纱一角
  • OpenLinkSaas 2025年第一季度开发计划
  • 【python小工具】怎么获取视频的关键帧频率?
  • 数字孪生可视化在各个行业的应用场景
  • Python实现windows自动关机
  • Go可以使用设计模式,但绝不是《设计模式》中的那样
  • 【C语言】_使用冒泡排序模拟实现qsort函数
  • openCvSharp 计算机视觉图片找茬
  • 从零开始开发纯血鸿蒙应用之处理外部文件
  • Spring中三级缓存详细讲解
  • 论文阅读:《Whole-animal connectomes of both Caenorhabditis elegans sexes》
  • 嵌入式开发之STM32学习笔记day03
  • windows10 安装 Golang 版本控制工具g与使用
  • SpringBoot 使用 Cache 集成 Redis做缓存保姆教程
  • R数据分析:多分类问题预测模型的ROC做法及解释
  • 数据结构与算法之二叉树: LeetCode 654. 最大二叉树 (Ts版)
  • Linux 容器漏洞
  • file与io流(1)
  • 忘记了PDF文件的密码,怎么办?
  • Linux权限管理(用户和权限之间的关系)