英文原文:https://opsive.com/support/documentation/ultimate-inventory-system/item-objects/
Item Objects(项目对象) 介绍
项目对象组件将特定项目链接到游戏对象。 游戏世界中可见的任何项目都应该有一个项目对象组件。 例如,物品拾取和装备物品都是物品在游戏世界中成形的方式。
ItemObject 组件非常灵活,因为它允许您将项目绑定/取消绑定到它。 因此,您可以轻松地将一些游戏对象与 ItemObject 合并在一起,并重用它们来生成场景中的任何项目。
当Item附加到项目对象或从项目对象分离时,会触发事件。 某些组件侦听该事件以更改 ItemObject 在游戏世界中的外观。
Item Object API
注册事件
// Register to the event of the Item changing on the Item Object
EventHandler.RegisterEvent(m_ItemObject, EventNames.c_ItemObject_OnItemChanged, HandleItemChanged);
private void HandleItemChanged(){
//The item changed
var itemInfo = m_ItemObject.ItemInfo;
}
将项目附加到项目对象
// Set the item to the Item Object
var newItem = InventorySystemManager.CreateItem("MyItemName");
// By default it will add 1 unit of the item.
m_ItemObject.SetItem(newItem);
// You may change the amount at any time.
m_ItemObject.SetAmount(5);
// Alternatively you may set an Item Info.
var itemInfo = (ItemInfo)(5, newItem);
m_ItemObject.SetItem(itemInfo);
从 Item 中获取 Item 对象
//如果您的项目是唯一的,您可以从项目中获取项目对象
var itemObject = newItem.GetLastItemObject();
// 您可以轻松检查项目是否绑定到项目对象。
if(newItem.IsBoundToItemObject(itemObject)){
//该 Item 绑定到该项目对象。
}
// 如果它不是唯一的,则该项目可能有多个项目对象:
// 循环遍历所有 item 对象
var count = newItem.GetItemObjectCount();
for(int i =0; i < count; i++){
var itemObjectAtIndex = newItem.GetItemObjectAt(i);
}