处理bug:
logic:为了更改物品的顺序,我们在Inventory manager里面记录了背包的序号,所以得到slots的序号就可以更改背包里的序号了 我希望当我拖拽起来的那一刹那我获得id,这样方便调整数据的ID InventoryManager里给slot数组表上序号,然后给item对应上 logic:物品调换时:首先把当前itemid给临时变量,然后把slot里的序号给当前的item,然后把temp里的临时变量给slot就是完成了对换(ID对换) logic:当前物品为空
slot设置的slotid有四处引用,
? ? ?public int slotID;//空格ID等于物品ID(Slot)
1inventorymanager重新生成就是给赋值,
for (int i = 0; i < instance.mabag.itemList.Count; i++)
{
//CreatNewItem(instance.mabag.itemList[i]);//重新创建回来了
instance.slots.Add(Instantiate(instance.emptyslot));
instance.slots[i].transform.SetParent(instance.slotgrid.transform);
instance.slots[i].GetComponent<Slot>().slotID = i;
instance.slots[i].GetComponent<Slot>().SetupSlot(instance.mabag.itemList[i]);
}
2拖拽时得到slotid
public void OnBeginDrag(PointerEventData eventData)
{
originalParent = transform.parent;
currentItemID = originalParent.GetComponent<Slot>().slotID;//当前拖拽的物品的id
transform.SetParent(transform.parent.parent);
transform.position = eventData.position;
GetComponent<CanvasGroup>().blocksRaycasts = false;
}
3.物品交换时
4.物品放到指定位置时
public void OnEndDrag(PointerEventData eventData)
{
if(eventData.pointerCurrentRaycast.gameObject.name=="Item")
{
transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform.parent);
transform.position = eventData.pointerCurrentRaycast.gameObject.transform.parent.position;
//改变背包中真实的数据,两个物品调换我们一般做法就是创建第三个临时变量,用来储存物品中的临时变量
var temp = mabag.itemList[currentItemID];
mabag.itemList[currentItemID] = mabag.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotID];
mabag.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotID] = temp;
eventData.pointerCurrentRaycast.gameObject.transform.position = originalParent.position;
eventData.pointerCurrentRaycast.gameObject.transform.SetParent(originalParent);
GetComponent<CanvasGroup>().blocksRaycasts = true;
return;
}
if (eventData.pointerCurrentRaycast.gameObject.name == "slot(Clone)")
{
//没有物品将他的位置设置到指定的位置就可以了
transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform);
transform.position = eventData.pointerCurrentRaycast.gameObject.transform.position;
GetComponent<CanvasGroup>().blocksRaycasts = true;
mabag.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotID] = mabag.itemList[currentItemID];
mabag.itemList[currentItemID] = null;
}
}
知识点整合:
CreateAssetMenu | 创建菜单项目 | | ? ? ? [TextArea] | 把一行字符串变成多行 | | ScriptableObject | 存储数据的类 | UnityEngine.ScriptableObject - Unity 脚本 API | transform.childcount | 父项具有子项的数量 | UnityEngine.Transform - Unity 脚本 API | transform.setparent | 设置父对象 | | transform.getchild | 根据索引得到子项 | | Tooptip | 可以在unity进行备注 | | gameObject.CompareTag | 匹配标签 | UnityEngine.GameObject - Unity 脚本 API | list<>.contains | 包含相同 | | transform.parent | 父对象 | | transform.setparent | 设置父对象 | | GetComponent<CanvasGroup>(). | GetComponent<CanvasGroup>().(字段,方法) | | using UnityEngine.EventSystems; public class ItemOnDrag : MonoBehaviour,IBeginDragHandler, IDragHandler, IEndDragHandler | 实现接口之后分别是开拖拽的一瞬间,在拖拽过程中,在拖拽结束后 | |
|