当前位置: 首页 > news >正文

UGUI 读取JSON动态克隆背包道具信息

Unity背包系统 - JSON读取完整实现

一、Excel转JSON方法

1.1 Excel表格格式示例

创建Items.xlsx,结构如下:

ItemNameItemImageNameItemCountItemNumberItemType
生命药水HealthPotion510011
魔力药水ManaPotion310021
长剑Sword120012

1.2 将Excel转为JSON的方法

方法一:使用在线转换工具

  1. 将Excel另存为CSV格式

  2. 使用CSV转JSON在线工具

  3. 保存为Items.json,放到Unity的Resources文件夹中

方法二:使用C#脚本转换
创建ExcelToJsonConverter.cs

using UnityEngine; using System.IO; #if UNITY_EDITOR using Excel = Microsoft.Office.Interop.Excel; #endif public class ExcelToJsonConverter : MonoBehaviour { #if UNITY_EDITOR [ContextMenu("Convert Excel to JSON")] public void ConvertExcelToJson() { string excelPath = Application.dataPath + "/Items.xlsx"; string jsonPath = Application.dataPath + "/Resources/Items.json"; Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Open(excelPath); Excel.Worksheet worksheet = workbook.Sheets[1]; // 读取数据转换为JSON // ... 转换代码 ... workbook.Close(); excelApp.Quit(); } #endif }

二、完整的JSON读取实现

2.1 JSON数据结构类

// ItemData.cs - JSON数据类 using System; using System.Collections.Generic; [Serializable] public class ItemList { public List<ItemJsonData> items; } [Serializable] public class ItemJsonData { public string itemName; public string imageName; // 图片文件名 public int itemCount; public int itemNumber; public int itemType; }

2.2 道具信息类(优化版)

// itemonfro.cs - 道具信息类 using UnityEngine; [System.Serializable] public class itemonfro { public string Itemname; public Sprite ItemImage; public int ItemCount; public int ItemNumBu; public int ItemType; // 空构造函数供JSON解析使用 public itemonfro() { } // 构造函数 public itemonfro(string name, Sprite image, int count = 1, int id = 0, int type = 0) { Itemname = name; ItemImage = image; ItemCount = count; ItemNumBu = id; ItemType = type; } }

2.3 完整的ItemClone类

using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class ItemClone : MonoBehaviour { [Header("UI组件")] public GameObject ItemGridPre; // 道具格子预制体 public RectTransform ItemGridParent; // Content父物体 [Header("资源管理")] public List<Sprite> ItemImageInfro; // 所有道具图片列表 public string jsonFilePath = "Items"; // JSON文件路径(不带后缀) [Header("道具数据")] public itemonfro[] ItemAllInfo; // 道具信息数组 // 图片名称映射字典 private Dictionary<string, Sprite> spriteDictionary = new Dictionary<string, Sprite>(); private void Awake() { // 1. 初始化图片字典 InitializeSpriteDictionary(); // 2. 从JSON加载数据 LoadDataFromJSON(); // 3. 克隆道具格子 CloneItem(); } // 初始化图片字典 void InitializeSpriteDictionary() { spriteDictionary.Clear(); foreach (Sprite sprite in ItemImageInfro) { if (sprite != null && !spriteDictionary.ContainsKey(sprite.name)) { spriteDictionary.Add(sprite.name, sprite); } } } // 从JSON文件加载数据 void LoadDataFromJSON() { // 1. 加载JSON文件 TextAsset jsonFile = Resources.Load<TextAsset>(jsonFilePath); if (jsonFile == null) { Debug.LogError($"找不到JSON文件: {jsonFilePath}"); CreateDefaultItems(); return; } try { // 2. 解析JSON数据 ItemList itemList = JsonUtility.FromJson<ItemList>(jsonFile.text); if (itemList == null || itemList.items == null) { Debug.LogError("JSON解析失败,使用默认数据"); CreateDefaultItems(); return; } // 3. 创建itemonfro数组 ItemAllInfo = new itemonfro[itemList.items.Count]; for (int i = 0; i < itemList.items.Count; i++) { ItemJsonData jsonData = itemList.items[i]; // 创建新的itemonfro对象 ItemAllInfo[i] = new itemonfro(); // 赋值数据 ItemAllInfo[i].Itemname = jsonData.itemName; ItemAllInfo[i].ItemCount = jsonData.itemCount; ItemAllInfo[i].ItemNumBu = jsonData.itemNumber; ItemAllInfo[i].ItemType = jsonData.itemType; // 通过图片名称查找Sprite if (!string.IsNullOrEmpty(jsonData.imageName)) { if (spriteDictionary.ContainsKey(jsonData.imageName)) { ItemAllInfo[i].ItemImage = spriteDictionary[jsonData.imageName]; } else { Debug.LogWarning($"找不到图片: {jsonData.imageName},使用默认图片"); ItemAllInfo[i].ItemImage = GetDefaultSprite(); } } else { ItemAllInfo[i].ItemImage = GetDefaultSprite(); } } Debug.Log($"成功加载 {ItemAllInfo.Length} 个道具"); } catch (System.Exception e) { Debug.LogError($"JSON解析错误: {e.Message}"); CreateDefaultItems(); } } // 获取默认Sprite Sprite GetDefaultSprite() { if (ItemImageInfro != null && ItemImageInfro.Count > 0) return ItemImageInfro[0]; return null; } // 创建默认道具数据(JSON加载失败时使用) void CreateDefaultItems() { ItemAllInfo = new itemonfro[5]; ItemAllInfo[0] = new itemonfro("生命药水", GetDefaultSprite(), 5, 1001, 1); ItemAllInfo[1] = new itemonfro("魔力药水", GetDefaultSprite(), 3, 1002, 1); ItemAllInfo[2] = new itemonfro("长剑", GetDefaultSprite(), 1, 2001, 2); ItemAllInfo[3] = new itemonfro("盾牌", GetDefaultSprite(), 1, 2002, 2); ItemAllInfo[4] = new itemonfro("金币", GetDefaultSprite(), 999, 3001, 3); } // 克隆道具到UI void CloneItem() { if (ItemAllInfo == null || ItemAllInfo.Length == 0) { Debug.LogError("没有道具数据可显示"); return; } // 清空现有道具格子 foreach (Transform child in ItemGridParent) { Destroy(child.gameObject); } // 克隆新道具格子 for (int i = 0; i < ItemAllInfo.Length && i < 100; i++) // 限制最多100个 { // 实例化预制体 GameObject TempItemGrid = Instantiate(ItemGridPre, ItemGridParent); TempItemGrid.name = $"Item_{ItemAllInfo[i].Itemname}_{i}"; try { // 获取UI组件并赋值 TextMeshProUGUI nameText = TempItemGrid.transform .GetChild(2) // 假设第3个子物体是名称容器 .GetChild(0) .GetChild(0) .GetComponent<TextMeshProUGUI>(); Image itemImage = TempItemGrid.transform .GetChild(3) // 假设第4个子物体是图片 .GetComponent<Image>(); // 赋值数据 if (nameText != null) nameText.text = ItemAllInfo[i].Itemname; if (itemImage != null && ItemAllInfo[i].ItemImage != null) itemImage.sprite = ItemAllInfo[i].ItemImage; // 设置计数显示(可选) Transform countTransform = TempItemGrid.transform.Find("ItemCount"); if (countTransform != null) { TextMeshProUGUI countText = countTransform.GetComponent<TextMeshProUGUI>(); if (countText != null) { countText.text = ItemAllInfo[i].ItemCount > 1 ? ItemAllInfo[i].ItemCount.ToString() : ""; } } } catch (System.Exception e) { Debug.LogError($"设置道具 {i} 时出错: {e.Message}"); } } } // 重新加载数据(可在编辑器测试用) [ContextMenu("重新加载JSON数据")] void ReloadData() { LoadDataFromJSON(); CloneItem(); } }

三、JSON文件示例

{ "items": [ { "itemName": "生命药水", "imageName": "HealthPotion", "itemCount": 5, "itemNumber": 1001, "itemType": 1 }, { "itemName": "魔力药水", "imageName": "ManaPotion", "itemCount": 3, "itemNumber": 1002, "itemType": 1 }, { "itemName": "长剑", "imageName": "Sword", "itemCount": 1, "itemNumber": 2001, "itemType": 2 }, { "itemName": "盾牌", "imageName": "Shield", "itemCount": 1, "itemNumber": 2002, "itemType": 2 }, { "itemName": "钥匙", "imageName": "Key", "itemCount": 3, "itemNumber": 3001, "itemType": 3 } ] }

、使用步骤

  1. 准备图片资源

    • 将道具图片导入Unity

    • 图片命名与JSON中的imageName一致

    • 将所有图片拖到ItemImageInfro列表中

  2. 创建JSON文件

    • Assets/Resources文件夹中创建Items.json

    • 按格式编写道具数据

  3. 设置UI组件

    • 将预制体和Content拖到对应位置

    • 确保预制体结构与代码中的GetChild索引匹配

  4. 运行测试

    • 运行游戏即可看到从JSON加载的道具

    • 修改JSON文件后,可在编辑器中使用"重新加载JSON数据"菜单更新

五、注意事项

  1. 图片查找:确保JSON中的imageName与导入的Sprite名称完全一致

  2. JSON格式:必须严格按照JSON格式,可使用JSON验证工具

  3. 路径问题:JSON文件必须放在Resources文件夹内

  4. 预制体结构:确保预制体结构与代码中的GetChild索引匹配

这样就完成了从Excel到JSON再到Unity背包的完整流程!

http://www.cnnetsun.cn/news/81895.html

相关文章:

  • 解密AI智能体通信黑盒:从混乱到高效协作的完整指南
  • 这个信号很明显:AI健康,开始换打法了
  • TikZJax终极指南:在浏览器中直接运行LaTeX绘图
  • ndb调试器完整教程:从基础使用到高级调试的终极指南
  • Auto-Subtitle完整教程:5分钟学会为视频添加智能字幕
  • 5个简单步骤:掌握Visual Studio许可证到期日期的管理秘诀
  • 友达 G185XW01 V1 工业液晶显示屏:18.5 英寸宽温高响应场景的显示驱动技术解析
  • 正交实验设计在软件测试用例生成中的应用研究
  • 17、Unix Shell编程:临时文件、数据读写与环境变量详解
  • 校园实验室|基于springboot + vue校园实验室管理系统(源码+数据库+文档)
  • 25、深入探索Shell交互与非标准特性
  • Apache Mesos运维实战:集群管理完整指南与故障处理方案
  • FlutterFire Remote Config用户细分实战:精准触达不同用户群体
  • Python 开发 - Python 装饰器(装饰器概述、函数概念、装饰器手动实现、装饰器语法糖实现)
  • 太阳能电池串IV检测系统:精准契合行业标准,筑牢光伏质量防线
  • 64、Ubuntu 下 C/C++ 编程与 Mono 开发全解析
  • 5、Ubuntu系统网络与图形界面使用指南
  • 快速构建MCP工具的开发包FastMCP
  • 推荐字节的文档图像解析工具Dolphin
  • 查 Intel CPU 信息不用绕弯!这个专属查询工具,精准直达官网详情~
  • MediaCreationTool 报错?用 Rufus 一键制作 Windows 启动 U 盘,兼容 Win10/11!
  • Dify平台提示词调试功能提升AI输出质量实测
  • Java JDK下载+安装+配置环境(详细教程含图片),小白收藏这篇就够了
  • 前端性能优化之大文件上传,零基础入门到精通,收藏这篇就够了
  • 37、Windows 8 安全与诊断实用指南
  • 蛋白质丙酰化修饰在代谢调控与疾病研究中的进展与应用
  • C# + LiveCharts 工业监控界面,实时数据可视化实战
  • 在数字中国建设大潮中,科技管理部门如何借助靶向的知识产权智能运营平台解决客户流失率高,达成重塑差异化服务优势,最终重塑健全长效运营机制?
  • 如何确保服务器的安全性
  • 获取JD商品详情数据 get_item_pro