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集合之Iterator迭代器实现代码解析

《Java集合之Iterator迭代器实现代码解析》迭代器Iterator是Java集合框架中的一个核心接口,位于java.util包下,它定义了一种标准的元素访问机制,为各种集合类型提供了一种统一的... 目录一、什么是Iterator二、Iterator的核心方法三、基本使用示例四、Iterator的工

Java 线程池+分布式实现代码

《Java线程池+分布式实现代码》在Java开发中,池通过预先创建并管理一定数量的资源,避免频繁创建和销毁资源带来的性能开销,从而提高系统效率,:本文主要介绍Java线程池+分布式实现代码,需要... 目录1. 线程池1.1 自定义线程池实现1.1.1 线程池核心1.1.2 代码示例1.2 总结流程2. J

linux系统中java的cacerts的优先级详解

《linux系统中java的cacerts的优先级详解》文章讲解了Java信任库(cacerts)的优先级与管理方式,指出JDK自带的cacerts默认优先级更高,系统级cacerts需手动同步或显式... 目录Java 默认使用哪个?如何检查当前使用的信任库?简要了解Java的信任库总结了解 Java 信

JS纯前端实现浏览器语音播报、朗读功能的完整代码

《JS纯前端实现浏览器语音播报、朗读功能的完整代码》在现代互联网的发展中,语音技术正逐渐成为改变用户体验的重要一环,下面:本文主要介绍JS纯前端实现浏览器语音播报、朗读功能的相关资料,文中通过代码... 目录一、朗读单条文本:① 语音自选参数,按钮控制语音:② 效果图:二、朗读多条文本:① 语音有默认值:②

Vue实现路由守卫的示例代码

《Vue实现路由守卫的示例代码》Vue路由守卫是控制页面导航的钩子函数,主要用于鉴权、数据预加载等场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、概念二、类型三、实战一、概念路由守卫(Navigation Guards)本质上就是 在路

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

JAVA实现Token自动续期机制的示例代码

《JAVA实现Token自动续期机制的示例代码》本文主要介绍了JAVA实现Token自动续期机制的示例代码,通过动态调整会话生命周期平衡安全性与用户体验,解决固定有效期Token带来的风险与不便,感兴... 目录1. 固定有效期Token的内在局限性2. 自动续期机制:兼顾安全与体验的解决方案3. 总结PS

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

Python屏幕抓取和录制的详细代码示例

《Python屏幕抓取和录制的详细代码示例》随着现代计算机性能的提高和网络速度的加快,越来越多的用户需要对他们的屏幕进行录制,:本文主要介绍Python屏幕抓取和录制的相关资料,需要的朋友可以参考... 目录一、常用 python 屏幕抓取库二、pyautogui 截屏示例三、mss 高性能截图四、Pill

使用MapStruct实现Java对象映射的示例代码

《使用MapStruct实现Java对象映射的示例代码》本文主要介绍了使用MapStruct实现Java对象映射的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、什么是 MapStruct?二、实战演练:三步集成 MapStruct第一步:添加 Mave