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(四)自定义代码篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/354133

相关文章

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当