【Unity】QFramework通用背包系统优化:使用Odin优化编辑器

本文主要是介绍【Unity】QFramework通用背包系统优化:使用Odin优化编辑器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

在学习凉鞋老师的课程《QFramework系统设计:通用背包系统》第四章时,笔者使用了Odin插件,对Item和ItemDatabase的SO文件进行了一些优化,使物品页面更加紧凑、更易拓展。

核心逻辑和功能没有改动,整体代码量减少了,并且增加了一个复制ItemConfig的小功能。

需要注意:

  • 在ItemConfigGroup的列表中中删除ItemConfig时,应该点红色的X按钮,不要点最右侧的叉号,不然关联的ItemConfig SO文件不会被同时删除;
  • QFramework带有的自定义属性功能可能会和Odin冲突,建议只使用其中一种;

为了和原教程区分,下文将使用ItemConfig和ItemConfigGroup类来代替Item和ItemDatabase类。

修改前后对比:
请添加图片描述

代码

IItem接口:

using UnityEngine;namespace QFramework
{public interface IItem{string GetKey { get; }string GetName { get; }string GetDescription { get; }Sprite GetIcon { get; }bool GetStackable { get; }bool GetHasMaxStackableCount { get; }int GetMaxStackableCount { get; }ItemLanguagePackage.LocalItem LocalItem { get; set; }bool GetBoolean(string propertyName);}
}

ItemConfig类:

using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;namespace QFramework
{[CreateAssetMenu(menuName = "@ItemKit/Create ItemConfig")]public class ItemConfig : ScriptableObject, IItem{public ItemConfigGroup ItemConfigGroup { get; set; }[HideLabel][PreviewField(48, ObjectFieldAlignment.Left)][HorizontalGroup("名称类型", 54), VerticalGroup("名称类型/left")]public Sprite Icon = null;private void OnValidate(){this.name = Key;}[VerticalGroup("名称类型/left")][Button("X"), GUIColor(1, 0, 0)]private void RemoveThisConfig(){if (EditorUtility.DisplayDialog("删除物品", "确定要删除吗?\n(此操作不可恢复)", "删除", "取消")){ItemConfigGroup.ItemConfigs.Remove(this);AssetDatabase.RemoveObjectFromAsset(this);AssetDatabase.SaveAssets();AssetDatabase.Refresh();}}[VerticalGroup("名称类型/left")][Button("Dup"), GUIColor("yellow")]private void DuplicateThisConfig() // 增加复制/插入功能{if (ItemConfigGroup == null){Debug.LogError("ItemConfigGroup is null!");return;}ItemConfigGroup.DuplicateItemConfig(ItemConfigGroup.ItemConfigs.IndexOf(this), this);}[VerticalGroup("名称类型/right"), LabelWidth(42)][LabelText("名称")]public string Name = string.Empty;[VerticalGroup("名称类型/right"), LabelWidth(42)][LabelText("描述")][TextArea(minLines: 1, maxLines: 4)]public string Description = string.Empty;[VerticalGroup("名称类型/right"), LabelWidth(42)][LabelText("关键字")]public string Key = string.Empty;[VerticalGroup("名称类型/right"), LabelWidth(42)][LabelText("是武器")]public bool IsWeapon = false;[HorizontalGroup("属性")][VerticalGroup("属性/stackable"), LabelWidth(66)][LabelText("可堆叠")]public bool IsStackable = true;[ShowIf("IsStackable")][VerticalGroup("属性/stackable"), LabelWidth(66)][Indent][LabelText("有最大值")]public bool HasMaxStackableCount = false;[ShowIf("IsStackable"), EnableIf("HasMaxStackableCount")][DisplayIf(new string[] { "IsStackable", "HasMaxStackableCount" }, new[] { false, false })][VerticalGroup("属性/stackable"), LabelWidth(66)][Indent(2)][LabelText("最大值")]public int MaxStackableCount = 99;public string GetName => ItemKit.CurrentLanguage == ItemKit.DefaultLanguage ? Name : LocalItem.Name;public string GetKey => Key;public string GetDescription => ItemKit.CurrentLanguage == ItemKit.DefaultLanguage ? Description : LocalItem.Description;public Sprite GetIcon => Icon;public bool GetStackable => IsStackable;public bool GetHasMaxStackableCount => HasMaxStackableCount;public int GetMaxStackableCount => MaxStackableCount;public ItemLanguagePackage.LocalItem LocalItem { get; set; }public bool GetBoolean(string propertyName){if (propertyName == "IsWeapon"){return IsWeapon;}return false;}}
}

ItemConfigGroup类:

using Sirenix.OdinInspector;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using UnityEditor;namespace QFramework
{[CreateAssetMenu(menuName = "@ItemKit/Create Item ConfigGroup")]public class ItemConfigGroup : ScriptableObject{public string NameSpace = "QFramework.Example";[Searchable][TableList(ShowIndexLabels = true)]public List<ItemConfig> ItemConfigs = new List<ItemConfig>();[Button("添加 ItemConfig", ButtonSizes.Large), GUIColor("yellow")]private void AddItemConfig(){// 创建一个新的 ItemConfig 实例ItemConfig itemConfig = CreateInstance<ItemConfig>();itemConfig.ItemConfigGroup = this;itemConfig.name = nameof(ItemConfig);itemConfig.Name = "新物品";itemConfig.Key = "item_new";// 将新创建的 itemConfig 添加到 ItemConfigGroup 的资源中AssetDatabase.AddObjectToAsset(itemConfig, this);// 在 ItemConfigs 列表中添加一个新的元素ItemConfigs.Add(itemConfig);// 保存所有更改到资源AssetDatabase.SaveAssets();// 刷新资源AssetDatabase.Refresh();}public void DuplicateItemConfig(int index, ItemConfig itemConfig){// 创建一个新的 ItemConfig 实例ItemConfig itemConfigSO = CreateInstance<ItemConfig>();itemConfigSO.ItemConfigGroup = this;itemConfigSO.name = itemConfig.Key;itemConfigSO.Name = string.Empty;itemConfigSO.Key = "item_new";itemConfigSO.IsWeapon = itemConfig.IsWeapon;itemConfigSO.IsStackable = itemConfig.IsStackable;itemConfigSO.HasMaxStackableCount = itemConfig.HasMaxStackableCount;itemConfigSO.MaxStackableCount = itemConfig.MaxStackableCount;// 将新创建的 itemConfig 添加到 ItemConfigGroup 的资源文件中AssetDatabase.AddObjectToAsset(itemConfigSO, this);// 在 ItemConfigs 列表中添加一个新的元素ItemConfigs.Insert(index + 1, itemConfigSO);// 保存所有更改到资源AssetDatabase.SaveAssets();// 刷新资源AssetDatabase.Refresh();}[Button("生成 Items 代码", ButtonSizes.Large), GUIColor("green")]private void GenerateCode(){var itemDatabase = this;// 获取当前 ItemDatabase 脚本的文件路径,并确定生成代码的保存位置string filePath = AssetDatabase.GetAssetPath(itemDatabase).GetFolderPath() + "/Items.cs";// 使用 QFramework 中的代码生成功能// 创建一个代码作用域树,用于生成代码结构ICodeScope rootCode = new RootCode()// 添加命名空间.Using("UnityEngine").Using("QFramework")// 空一行.EmptyLine()// 定义命名空间.Namespace(itemDatabase.NameSpace, ns =>{// 在命名空间中定义一个类ns.Class("Items", String.Empty, false, false, c =>{// 为每个 itemDB.ItemConfigs 生成一个静态字符串字段foreach (ItemConfig itemConfig in itemDatabase.ItemConfigs){c.Custom($"public static string {itemConfig.Key} = \"{itemConfig.Key}\";");Debug.Log(itemConfig.Key);}});});// 创建或覆盖文件,并准备写入生成的代码// 使用 using 语句自动管理 StreamWriter 的生命周期。// 当离开 using 代码块的作用域时,fileWriter 的 Dispose 方法会被自动调用,确保文件资源被正确关闭。using StreamWriter fileWriter = File.CreateText(filePath);// 创建一个代码写入器,将代码作用域树转换为字符串FileCodeWriter codeWriter = new FileCodeWriter(fileWriter);// 生成代码并写入文件rootCode.Gen(codeWriter);// 保存所有未保存的资源更改AssetDatabase.SaveAssets();// 刷新 Unity 编辑器的资源数据库AssetDatabase.Refresh();}private void OnValidate(){foreach (ItemConfig itemConfig in ItemConfigs){if (itemConfig != null){itemConfig.name = itemConfig.Key;}elseItemConfigs.Remove(itemConfig);}}}
}

这篇关于【Unity】QFramework通用背包系统优化:使用Odin优化编辑器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、