unity库存系统插件-Ultimate Inventory System(四)自定义代码篇

本文主要是介绍unity库存系统插件-Ultimate Inventory System(四)自定义代码篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 一、库存接口
    • 1、获得库存接口
    • 2、获得库存总体属性(方便计算角色属性加成)
  • 二、物品接口
    • 1、物品
    • 2、货币
    • 3、获取并设置物品属性
    • 4、自定义随机物品接口
  • 三、UI面板
  • 四、交互(物品穿戴)
  • 五、保存加载
  • 总结
  • 感想


前言

这篇为自定义视图代码篇,本篇会从项目的角度重新规划功能
添加删除物品的接口、数据联动(喝血药)、物品联动(与模型联动-穿戴)、捡取东西的优化、打怪爆装备的接口、加载保存接口、宝箱随机功能。


一、库存接口

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)
{//类型未知,故而使用objectvar 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整合完成后当然是删个干净。

感想

本来还想多写些,把自己想的具体需求写出来,但是重构过程中我扪心自问,这些基础够了吗?够了。够了吗?做游戏还不够!假设游戏世界中每个人都有自己的库存,可以交易,角色的属性可以通过装备的物品增强属性/改变外观,爆装备还是搜尸体,具体怎么交互,这些都需要自己实现。
而这些不是一尘不变的,需要设计师不断试错才能确定,我们没必要将自己想做的东西完整的说出来,将怎么做类似的功能告诉大家;做游戏就好比用一堆颜色不同的石子拼图,我们只需要给想做的人一堆石子并告诉她怎么拼就可以了。

这篇关于unity库存系统插件-Ultimate Inventory System(四)自定义代码篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/qq_41912124/article/details/130215872
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/354133

相关文章

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam