文章目录
前言
这篇为自定义视图代码篇,本篇会从项目的角度重新规划功能
添加删除物品的接口、数据联动(喝血药)、物品联动(与模型联动-穿戴)、捡取东西的优化、打怪爆装备的接口、加载保存接口、宝箱随机功能。
一、库存接口
1、获得库存接口
protected uint m_InventoryID = 1;
void Start(){
var inventory = InventorySystemManager.GetInventoryIdentifier(m_InventoryID).Inventory;
}
2、获得库存总体属性(方便计算角色属性加成)
(1)通常情况人物由自身和装备属性组成,以下api可以快速获取库存内的物品属性集合
//attributeName为属性名称,例如填入“灵气”这类属性值
var inventory = InventorySystemManager.GetInventoryIdentifier(m_InventoryID).Inventory;
var sum = inventory.GetFloatSum(attributeName);
(2)获得库存中的集合类型
var EquipmentCollection = inventory.GetItemCollection("装备栏");
var ItemCollection1 = inventory.GetItemCollection("ItemCollection1");
EquipmentCollection.GetFloatSum(attributeName);
EquipmentCollection.GetFloatSum(attributeName);
二、物品接口
Item是一个用于引用Item实例的类。它通常存储在库存中。
///如果Item是可变的,它可以更改属性值。
///如果项是不可变的,则它只能在创建时更改其属性
1、物品
物品集合由物品堆栈列表组成。默认物品集合允许每个物品单个物品堆栈,但在多物品集合的情况下,每个物品可能有多个物品堆栈。例如,你可能有两个堆栈“5个苹果”和“10个苹果”。“苹果”物品无法区分,因为它们是相同的,但堆栈是不同的,每个堆栈都有自己的参考。因此,例如,如果您想删除“3个苹果”,您可以从“5个苹果”或“10个苹果”堆栈中选择这样做。
//创建新的物品-多种方式(显式或隐式)
new ItemInfo(itemAmount, itemCollection, itemStack);
new ItemInfo("MyItem", 1);
new ItemInfo(item, amount);
new ItemInfo(itemAmount);
new ItemInfo(itemStack);
new ItemInfo(itemAmount, otherItemInfo);
new ItemInfo(amount, otherItemInfo);
new ItemInfo(itemDefinition, amount);
new ItemInfo(otherItemInfo, amount);
new ItemInfo(amount, item);
using Opsive.UltimateInventorySystem.Core;
using Opsive.UltimateInventorySystem.Core.DataStructures;
//给指定库存添加三柄飞剑
inventory.AddItem("飞剑", 3);
//从ItemCollection2集合删除2个铁
//获得库存接口
var inventory = InventorySystemManager.GetInventoryIdentifier(m_InventoryID).Inventory;
//从ItemCollection2集合中删除
var EquipmentCollection = inventory.GetItemCollection("ItemCollection2");
//首先获取您想要的项目。在这里我们将得到我们找到的第一堆铁。
var result = EquipmentCollection.GetItemInfo(InventorySystemManager.GetItemDefinition("铁"));
//如果结果没有值,则库存中没有铁
if (result.HasValue == false) { return; }
//我们现在有了铁的ItemInfo。
var itemInfo = result.Value;
//让我们最多删除两个铁
itemInfo = new ItemInfo(Mathf.Max(2, itemInfo.Amount), itemInfo);
inventory.RemoveItem(itemInfo);
//插件提供AddItem、RemoveItem、RemoveAllItems等接口,示例略,可自行尝试
2、货币
using Opsive.UltimateInventorySystem.Core.InventoryCollections;
using Opsive.UltimateInventorySystem.Exchange;
private CurrencyOwner m_CurrencyOwner;
void Awake(){
InventoryIdentifier inventoryIdentifier = InventorySystemManager.GetInventoryIdentifier(m_InventoryID);
m_CurrencyOwner = inventoryIdentifier.CurrencyOwner;
//调用
AddCurrency("上品灵石", 20);
RemoveCurrency("上品灵石", 10);
}
public void AddCurrency(string currencyName,int amount)
{
m_CurrencyOwner.AddCurrency(currencyName,amount);
}
public void RemoveCurrency(string currencyName, int amount)
{
m_CurrencyOwner.RemoveCurrency(currencyName, amount);
}
3、获取并设置物品属性
(1)获得物品的属性
using Opsive.UltimateInventorySystem.Core.AttributeSystem;
/// <summary>
/// 打印物品的属性值
/// </summary>
/// <param name="inventory"></param>
/// <param name="itemName"></param>
/// <param name="attributeName"></param>
void GetItemAttributeValue(Inventory inventory,string itemName,string attributeName) {
var item = GetItem(inventory,itemName);
var value = GetItemAttributeAsObject(item, attributeName);
Debug.Log(value);
}
/// <summary>
/// 获得物品
/// </summary>
/// <param name="inventory"></param>
/// <param name="itemName"></param>
Item GetItem(Inventory inventory,string itemName) {
Item item;
//获得物品定义
ItemDefinition itemDefinition = InventorySystemManager.GetItemDefinition(itemName);
//判空
if (itemDefinition == null)
{
Debug.LogError("该系统木有这玩野:"+itemName);
}
//获得传入库存中与之匹配的第一个物品
var itemInfo = inventory.GetItemInfo(itemDefinition);
//判空
if (itemInfo.HasValue == false)
{
Debug.LogError("该库存木有这玩野:" + itemName);
//不包含,但也可以从默认物品获得属性
item = itemDefinition.DefaultItem;
}
else
{
item = itemInfo.Value.Item;
}
return item;
}
/// <summary>
/// 获得物品属性
/// </summary>
/// <param name="item"></param>
/// <param name="attributeName"></param>
/// <returns></returns>
object GetItemAttributeAsObject(Item item, string attributeName)
{
//类型未知,故而使用object
var attribute = item.GetAttribute(attributeName);
if (attribute == null)
{
Debug.LogError($"物品 '{item.name}' 木有找到名字为 '{attributeName}'的属性");
return null;
}
var attributeValue = attribute.GetValueAsObject();
return attributeValue;
}
private float GetItemAttributeAsFloat(Item item, string attributeName)
{
var floatAttribute = item.GetAttribute<Attribute<float>>(attributeName);
if (floatAttribute == null)
{
Debug.LogError($"物品 '{item.name}' 木有找到float类型的 '{attributeName}'的属性");
return float.NaN;
}
return floatAttribute.GetValue();
}
private string GetItemAttributeAsString(Item item, string attributeName)
{
var stringAttribute = item.GetAttribute<Attribute<string>>(attributeName);
if (stringAttribute == null)
{
Debug.LogError($"物品 '{item.name}' 木有找到string类型的 '{attributeName}'的属性");
return null;
}
return stringAttribute.GetValue();
}
private void GetItemAttributeAsInt(Item item, string attributeName)
{
var intAttribute = item.GetAttribute<Attribute<int>>(attributeName);
if (intAttribute == null)
{
Debug.LogError($"物品 '{item.name}' 木有找到int类型的 '{attributeName}'的属性");
return;
}
intAttribute.GetValue();
}
调用
//获得飞剑的属性描述并打印
var inventory = InventorySystemManager.GetInventoryIdentifier(1).Inventory;
GetItemAttributeValue(inventory, "飞剑", "描述");
(2)设置物品属性
void SetItemAttributeValue(Inventory inventory, string itemName, string attributeName,string value) {
var item = GetItem(inventory, itemName);
SetItemAttributeAsObject(item , attributeName, value);
}
void SetItemAttributeAsObject(Item item,string attributeName,string attributeValueAsStringObject) {
//https://opsive.com/support/documentation/ultimate-inventory-system/save-system/
if (item.IsMutable == false)
{
Debug.Log("不可变物品");
return;
}
var itemAttribute = item.GetAttribute(attributeName);
if (itemAttribute == null)
{
Debug.Log($"物品中没有'{item.name}'属性");
return;
}
if (itemAttribute.AttachedItem == null)
{
Debug.Log($"'{attributeName}'不是一个属性");
return;
}
//不知道属性类型,用string设置
itemAttribute.SetOverrideValueAsObject(attributeValueAsStringObject);
}
private void SetItemAttributeAsInt(Item item, string attributeName, int attributeValue)
{
var intAttribute = item.GetAttribute<Attribute<int>>(attributeName);
if (intAttribute == null)
{
Debug.Log($"物品中没有int类型的'{item.name}'属性");
return;
}
intAttribute.SetOverrideValue(attributeValue);
}
private void SetItemAttributeAsString(Item item, string attributeName, string attributeValue)
{
var stringAttribute = item.GetAttribute<Attribute<string>>(attributeName);
if (stringAttribute == null)
{
Debug.Log($"物品中没有string类型的'{item.name}'属性");
return;
}
stringAttribute.SetOverrideValue(attributeValue);
}
private void SetItemAttributeAsFloat(Item item, string attributeName, float attributeValue)
{
var floatAttribute = item.GetAttribute<Attribute<float>>(attributeName);
if (floatAttribute == null)
{
Debug.Log($"物品中没有float类型的'{item.name}'属性");
return;
}
floatAttribute.SetOverrideValue(attributeValue);
}
调用
//修改飞剑的描述属性为:修改了
//如果有多把飞剑将会修改第一个,其余不变
var inventory = InventorySystemManager.GetInventoryIdentifier(1).Inventory;
SetItemAttributeValue(inventory,"飞剑","描述","修改了");
4、自定义随机物品接口
通常游戏中物品的属性是固定且稳定的,但从多年以前像暗黑破坏神之类的游戏越来越多,其中敌人有概率掉落一个具有随机稀有度和攻击属性的物品。随机攻击属性取决于稀有度,玩家不会想要攻击力低于普通物品的传奇物品。
不幸的是,这些通常需要按游戏定制。我这里提供一种写法,需要注意该类物品需要是可变的(基本属性设置为可变的,注意:要在运行时覆盖属性,该项目必须是可变且唯一的,也就是需要满足Mutable & Unique)。
代码-暂定(在进行调试和封装)
三、UI面板
第一种:
void Start(){
var displayPanelManager = InventorySystemManager.GetDisplayPanelManager(m_InventoryID);
displayPanelManager.OpenPanel("BagPanel");//打开背包
//displayPanelManager.ClosePanel("BagPanel");//关闭背包
}
第二种:
public DisplayPanel BagPanel;
void Start(){
BagPanel.SmartOpen();//打开背包
//displayPanel.SmartClose();//关闭背包
}
四、交互(物品穿戴)
参考官方的demo场景——14 Get Set Attribute Stat (With Code),穿戴装备通过Equipper,首先需要我们把物品添加一个GameObject的属性,子类继承它并设置对应物品的预制体。
采集和丢弃在demo场景——13 Pickups,人(怪物)死后爆出装备,宝箱功能在此基础上制作即可。
五、保存加载
1、通用接口
using Opsive.UltimateInventorySystem.SaveSystem;
//因为时序问题,这里写在Start里面
void Start(){
//保存在文件0中
SaveSystemManager.Save(0, true);
//加载到文件0中
SaveSystemManager.Load(0);
//删除文件0中的保存
SaveSystemManager.DeleteSave(0);
//输出保存路径 我的PC端为C:\Users\user\AppData\LocalLow\DefaultCompany\YYS,自行修改
SaveSystemManager.Instance.PrintSaveFolderPath();
//获取当前缓存的保存数据的状态
SaveDataInfo saveDataInfo = SaveSystemManager.GetCurrentSaveDataInfo();
//获取特定Saver组件的缓存序列化数据
SaveSystemManager.TryGetSaveData("密钥", out var serializedData);
Debug.Log(serializedData.Version);
}
2、重写或者自定义
参考以下链接即可,注意新版本的ISave好像替换为InventorySaver;怎么说呢,我觉得用官方自带的接口即可,既然用这种插件就代表着处于原型开发阶段,将官方的保存接口整合到自己项目的总保存接口当中,利大于弊。
文档链接
总结
此篇有些潦草,不少功能一笔带过,实在事出有因。
插件更新(最新为1.2.16)和其他的事情耽搁,本人准备重新规划和改动该库存插件的文章;目前的篇幅带着个人主观的思想探索,不够简练,很多说明没有必要,有些则是漏掉,导致写这篇的时候感觉有不少需要补充说明,写出来必定有割裂感。综上所述,故而某些功能一概而过。
具体计划第一篇不动,第二篇丰富细节,第三篇重构精简,第四篇补全,流程会重新走一遍确保无误。(最近研究两个有意思的功能,希望5月底能补完)
新版本文章重构基本完成,官方在新版本修复了不少bug并提供了新的Demo示例,导致部分我想完善的功能没必要写了,直接看官方案例就行了。
最后我会将官方的demo和文章的demo整合精简一下,至于官方的demo整合完成后当然是删个干净。
感想
本来还想多写些,把自己想的具体需求写出来,但是重构过程中我扪心自问,这些基础够了吗?够了。够了吗?做游戏还不够!假设游戏世界中每个人都有自己的库存,可以交易,角色的属性可以通过装备的物品增强属性/改变外观,爆装备还是搜尸体,具体怎么交互,这些都需要自己实现。
而这些不是一尘不变的,需要设计师不断试错才能确定,我们没必要将自己想做的东西完整的说出来,将怎么做类似的功能告诉大家;做游戏就好比用一堆颜色不同的石子拼图,我们只需要给想做的人一堆石子并告诉她怎么拼就可以了。