英文原文:https://opsive.com/support/documentation/ultimate-inventory-system/item-actions/
Item action(项目操作) 介绍
ITEM ACTIONS
项目操作用于对库存中的项目执行操作。 不应将其与 item object(项目对象)行为混淆,项目对象行为旨在使用游戏世界中的项目,并引用游戏对象(例如挥舞剑)。
项目删除操作 是项目操作的一个很好的示例。 它用于从库存中删除物品。
项目操作有两种主要方法。 CanInvoke(ItemInfo, ItemUser) 和 Invoke(ItemInfo, ItemUser)。 使用Item Info(项目信息),您可以创建自己的逻辑,让您选择何时可以或不可以执行操作。 如果可以执行该操作,则您可以访问项目属性以调用特定于所用项目的操作。 项目用户允许您传递对项目用户对象的引用,该对象通常位于玩家角色上。 如果您希望获得角色组件的引用,这非常有用。
项目操作可以随时随地使用,但它们主要用于Item View Slots Containers(项目视图槽容器),如Inventory Grid(库存网格)或Item Hotbar(项目热栏)。 通过项目操作集和类别项目操作集,您可以将项目类别映射到不同的项目操作。
Item action set(项目操作集)
项目操作集是一个可编写脚本的对象,可以通过Create -> Ultimate Inventory System -> Item Actions -> Item Action Set 来创建。 使用相同的路径,您将能够创建Category Item Action Set(类别项操作集)。
项目操作集允许您设置与指定的项目类别相关的项目操作列表。 例如,“Pickupable(可拾取)”项目类别可以有“Drop(丢弃)”操作。
您还可以将类别添加到排除类别。 类别项目操作集将使用这些类别来查找与项目匹配的所有项目操作。
Category item action set(类别项目操作集)
类别项目操作集是项目操作集的列表,“ItemViewSlotsContainerCategoryItemActionSetBinding”组件可以使用它来匹配所选项目的项目操作。 这使得 UI 能够知道某个项目可以使用哪些操作,并做出相应的行为。 例如,物品“剑”可以是“可装备”、“可拾取”和“热栏物品”物品类别的一部分。 这意味着“剑”可用的项目操作将是“装备”、“掉落”和“分配”,每个操作都来自不同的项目操作集对象。 库存网格检查器的项目操作列表将允许您在单击项目时显示可能的操作列表。
项目操作 API
创建自己的 Item Action 非常简单,只需创建一个继承 Item Action 的新类并重写 CanInvokeInternal 和 InvokeActionInternal 函数即可。
[System.Serializable]
public class MyItemAction : ItemAction
{
/// <summary>
/// Can the item action be invoked.
/// </summary>
/// <param name="itemInfo">The item info.</param>
/// <param name="itemUser">The item user (can be null).</param>
/// <returns>True if it can be invoked.</returns>
protected override bool CanInvokeInternal(ItemInfo itemInfo, ItemUser itemUser)
{
// Return true if the item can be invoked or false if it cannot.
return true;
}
/// <summary>
/// Consume the item.
/// </summary>
/// <param name="itemInfo">The item info.</param>
/// <param name="itemUser">The item user (can be null).</param>
protected override void InvokeActionInternal(ItemInfo itemInfo, ItemUser itemUser)
{
// Invoke the item action
}
}
有一些特殊的接口和抽象项目操作在创建自定义项目操作时特别有用。 这是一个列表:
- IActionWithPanel:允许您设置调用Item Action 的Panel。 示例:移动项目操作
- ItemActionWithQuantityPickerPanel:从prefab生成数量选择器面板,并在调用“真实”项目操作之前使用 async/await 获取数量。 示例:数量掉落物品操作。
- ItemViewSlotsContainerItemAction:引用项目视图槽容器的项目操作。 示例:打开其他项目视图槽容器项目操作。
- ItemActionWithAsyncFuncActionPanel<T>:生成一个 Async Func 操作面板,可用于显示选项列表。 示例:分配热栏项目操作
- ItemObjectAction :允许您引用项目对象,当项目用户无法从项目信息中识别相关项目对象时,这可能很有用。
创建自定义项目操作后,它将显示在类别项目操作对象的项目操作类型下拉列表中。
直接通过代码调用 Item Action,无需 UI
在下面的示例中,我们尝试放下两个苹果。
public void ManuallyCallDropItemAction()
{
//To call an item action directly in code you will need 3 things
//1) The ItemInfo
//2) The ItemAction
//3) The ItemUser
//First get the item you want. Here we'll get the first stack of apples we find.
var result = m_Inventory.GetItemInfo(InventorySystemManager.GetItemDefinition("Apple"));
//if the result does not have a value we do no have Apples in the inventory
if(result.HasValue == false){ return; }
//We have the apple ItemInfo now.
var appleItemInfo = result.Value;
//Lets trim to 2 apples maximum
appleItemInfo = new ItemInfo(Mathf.Max(2, appleItemInfo.Amount), appleItemInfo);
//Lets get the ItemUser. This is usually placed of the same gameobject as the Inventory, but not always
var itemUser = m_Inventory.GetComponent<ItemUser>();
//now lets create the drop item action, You can cache this object and reuse it if you which for more performance.
var dropItemAction = new DropItemAction();
//You must initialize the item action before using it. This can be only once
dropItemAction.Initialize(false);
//OPTIONAL: check if you are allowed to invoke the action (it will do this anyways internally)
var canInvoke = dropItemAction.CanInvoke(appleItemInfo, itemUser);
//Invoke the action
dropItemAction.InvokeAction(appleItemInfo, itemUser);
//对于某些操作,您甚至可以使用额外的数据
//例如,放置操作会跟踪最后放置的拾取对象
var droppedPickup = dropItemAction.PickUpGameObject;
}
注意:并非所有项目操作都可以像这样直接调用它们。 有些依赖于附加数据,例如 MoveItemAction 和其他一些继承 IActionWithPanel 并期望引用 ItemViewSlotContaner 或面板的数据。 这些都可以通过代码来设置。