Unity json反序列化为 字典存储

当在Unity游戏中需要加载和管理游戏数据,通常使用JSON文件是一种常见的方法。在本篇博客中,我们将深入探讨如何使用C#和Unity的JSON反序列化功能来实现这一目标。我们可以使用Unity的JsonUtility来反序列化JSON数据并将其映射到自定义的C#数据结构中。

首先,让我们来创建一些数据类,以便加载和管理游戏中的角色和武器数据。在这个示例中,我们将使用Player(角色)、Monster(怪物)和WeaponData(武器数据)这三种数据类型。

{"players": [{"id": "1","name": "player0","weaponID": "102","maxHp": "50","damage": "0","defense": "0","moveSpeed": "5.0","coolDown": "0","amount": "0"},{"id": "2","name": "player1","weaponID": "101","maxHp": "40","damage": "-10","defense": "0","moveSpeed": "5","coolDown": "20","amount": "0"},{"id": "3","name": "player2","weaponID": "101","maxHp": "50","damage": "20","defense": "1","moveSpeed": "5.0","coolDown": "-50","amount": "1"},{"id": "4","name": "player3","weaponID": "102","maxHp": "50","damage": "-50","defense": "-1","moveSpeed": "6","coolDown": "100","amount": "0"}]
} 

我们需要创建了具有与JSON数据匹配的数据结构。这些类使用[Serializable]特性,以便能够进行JSON序列化和反序列化

[Serializable]public class Monster{public int id;public string name;public int maxHp;public int damage;public int defense;public float moveSpeed;public int expMul;}`在这里插入代码片`

通过观察JSON文件我们发现,这个JSON文件示例是一个包含多个玩家信息的数组。

[Serializable]public class PlayerData{public List<Player> players = new List<Player>();}

我们可以用一个玩家数据结构的数组去存储这个json文件,并

TextAsset textAsset = Managers.Resource.Load<TextAsset>($"Data/PlayerData");
PlayerData []players JsonUtility.FromJson<Loader>(textAsset.text);

去解析并且遍历它放到字典里,
当我们要解析的json特别多时 我们定义了一个泛型方法LoadJson,该方法负责加载JSON数据并将其反序列化为具体类型的字典。这个方法接受一个Loader类型,该类型必须实现ILoader接口。

 Loader LoadJson<Loader, Key, Value>(string path) where Loader : ILoader<Key, Value>{TextAsset textAsset = Managers.Resource.Load<TextAsset>($"Data/{path}");return JsonUtility.FromJson<Loader>(textAsset.text);}

ILoader接口

public interface ILoader<Key, Value>
{Dictionary<Key, Value> MakeDict();
}

我们让存储具有JSON数据结构的数组继承该接口

	[Serializable]public class PlayerData : ILoader<int, Player>{public List<Player> players = new List<Player>();public Dictionary<int, Player> MakeDict(){Dictionary<int, Player> dict = new Dictionary<int, Player>();foreach (Player player in players)dict.Add(player.id, player);return dict;}}

这样就可以将JSON文件反序列化放在数组中

 public Dictionary<int, Data.WeaponData> WeaponData { get; private set; } = new Dictionary<int, Data.WeaponData>();public Dictionary<int, Data.Player> PlayerData { get; private set; } = new Dictionary<int, Data.Player>();public Dictionary<int, Data.Monster> MonsterData { get; private set; } = new Dictionary<int, Data.Monster>();public void Init(){PlayerData = LoadJson<Data.PlayerData, int, Data.Player>("PlayerData").MakeDict();WeaponData = LoadJson<Data.WeaponDataLoader, int, Data.WeaponData>("WeaponData").MakeDict();MonsterData = LoadJson<Data.MonsterData, int, Data.Monster>("MonsterData").MakeDict();}

代码如下

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public interface ILoader<Key, Value>
{Dictionary<Key, Value> MakeDict();
}public class DataManager
{public Dictionary<int, Data.WeaponData> WeaponData { get; private set; } = new Dictionary<int, Data.WeaponData>();public Dictionary<int, Data.Player> PlayerData { get; private set; } = new Dictionary<int, Data.Player>();public Dictionary<int, Data.Monster> MonsterData { get; private set; } = new Dictionary<int, Data.Monster>();public void Init(){PlayerData = LoadJson<Data.PlayerData, int, Data.Player>("PlayerData").MakeDict();WeaponData = LoadJson<Data.WeaponDataLoader, int, Data.WeaponData>("WeaponData").MakeDict();MonsterData = LoadJson<Data.MonsterData, int, Data.Monster>("MonsterData").MakeDict();}Loader LoadJson<Loader, Key, Value>(string path) where Loader : ILoader<Key, Value>{TextAsset textAsset = Managers.Resource.Load<TextAsset>($"Data/{path}");return JsonUtility.FromJson<Loader>(textAsset.text);}}

在DataManager的Init方法中,我们加载并初始化了游戏数据,包括角色数据、武器数据和怪物数据。通过调用LoadJson泛型方法,我们可以轻松地加载各种类型的JSON数据并将其转化为字典对象。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;namespace Data
{#region Character[Serializable]public class Player{public int id;public string name;public int weaponID;public int maxHp;public int damage;public int defense;public float moveSpeed;public int coolDown;public int amount;}[Serializable]public class PlayerData : ILoader<int, Player>{public List<Player> players = new List<Player>();public Dictionary<int, Player> MakeDict(){Dictionary<int, Player> dict = new Dictionary<int, Player>();foreach (Player player in players)dict.Add(player.id, player);return dict;}}#endregion#region Monster[Serializable]public class Monster{public int id;public string name;public int maxHp;public int damage;public int defense;public float moveSpeed;public int expMul;}public class MonsterData : ILoader<int, Monster>{public List<Monster> monsters = new List<Monster>();public Dictionary<int, Monster> MakeDict(){Dictionary<int, Monster> dict = new Dictionary<int, Monster>();foreach (Monster monster in monsters)dict.Add(monster.id, monster);return dict;}}#endregion#region Weapon[Serializable]public class WeaponData{public int weaponID;public string weaponName;public List<WeaponLevelData> weaponLevelData = new List<WeaponLevelData>();}[Serializable]public class WeaponLevelData{public int level;public int damage;public float movSpeed;public float force;public float cooldown;public float size;public int penetrate;public int countPerCreate;}[Serializable]public class WeaponDataLoader : ILoader<int, WeaponData>{public List<WeaponData> weapons = new List<WeaponData>();public Dictionary<int, WeaponData> MakeDict(){Dictionary<int, WeaponData> dict = new Dictionary<int, WeaponData>();foreach (WeaponData weapon in weapons)dict.Add(weapon.weaponID, weapon);return dict;}}#endregion}

通过使用Unity的JsonUtility和C#的泛型方法,我们可以方便地加载和管理游戏数据。这种方法对于处理不同类型的数据非常有用,而且代码可复用性很高。希望这篇博客对你了解Unity中的JSON反序列化和数据管理有所帮助。如果你有任何问题或需要进一步的指导,请随时在评论中提问!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/134256.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CSDN 每日一练 ★☆☆】【链表】删除排序链表中的重复元素

【CSDN 每日一练 ★☆☆】【链表】删除排序链表中的重复元素 链表 递归 题目 存在一个按升序排列的链表&#xff0c;给你这个链表的头节点 head &#xff0c;请你删除所有重复的元素&#xff0c;使每个元素 只出现一次 。 返回同样按升序排列的结果链表。 示例 示例 1&am…

ActiveMq学习⑨__基于zookeeper和LevelDB搭建ActiveMQ集群

引入消息中间件后如何保证其高可用&#xff1f; 基于zookeeper和LevelDB搭建ActiveMQ集群。集群仅提供主备方式的高可用集群功能&#xff0c;避免单点故障。 http://activemq.apache.org/masterslave LevelDB&#xff0c;5.6版本之后推出了LecelDB的持久化引擎&#xff0c;它使…

基于Qt QProcess获取linux启动的程序、QScreen 截屏、GIF动画实现

在Linux中,可以使用QProcess类来获取已启动的程序。以下是一个示例代码: #include <QCoreApplication>#include <QProcess>int main(int argc, char *argv[]){QCoreApplication a(argc, argv); // 创建一个QProcess对象 QProcess process; // 设置执行…

kubernetes集群编排——k8s调度

nodename vim nodename.yaml apiVersion: v1 kind: Pod metadata:name: nginxlabels:app: nginxspec:containers:- name: nginximage: nginxnodeName: k8s2 nodeName: k8s2 #找不到节点pod会出现pending&#xff0c;优先级最高 kubectl apply -f nodename.yamlkubectl get pod …

Linux之打印函数调用依赖关系(六十一)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

linux地址空间

地址空间 内存空间示意图虚拟地址空间虚拟地址进程地址空间生命周期图解为什么要有地址空间呢&#xff1f; 小结 内存空间示意图 进程是在内存中运行的&#xff0c;为了便于管理&#xff0c;不同的数据会存储在不同的区域&#xff0c;因此内存就被分为几部分&#xff0c;如下图…

微型计算机原理1

一、选择题 1.8086CPU的字长是&#xff08;&#xff09;位。 A. 32 B. 128 C. 64 D. 16 2 间接寻址方式中&#xff0c;操作数在(&#xff09;中。 A. 通用寄存器 B. 内存单元 C. 程序计数器 D.堆栈 3.在循环指令LOOP和串操作指令中,用作计数器的寄存器是() A. AX B. BX C. C…

软件测试/测试开发丨如何利用ChatGPT自动生成测试用例思维导图

点此获取更多相关资料 简介 思维导图是一种用图形方式表示思维和概念之间关系的工具&#xff1a; 有些公司会使用思维导图编写测试用例&#xff0c;这样做的优点是&#xff1a; 1.可视化和结构化。 2.易于理解&#xff0c;提高效率。 而 ChatGPT 是无法直接生成 xmind 格式…

Linux--进程间通信

1.进程间通信 进程间通信的背景&#xff1a; 进程之间是相互独立的&#xff0c;进程由内核数据结构和它所对应的代码和数据&#xff0c;通信成本比较高。 进程间通信目的&#xff1a; 数据传输&#xff1a;一个进程需要将它的数据发送给另一个进程 资源共享&#xff1a;多个进程…

Visual Studio 2019光标变成灰色方块问题

文章目录 Visual Studio 2019光标变成灰色方块问题问题描述解决方案 Visual Studio 2019光标变成灰色方块问题 问题描述 单击和双击都无法选中单词&#xff0c;总是选择整行或者是当前光标处的前几个字符一起选中&#xff0c;没有规则&#xff0c;貌似选择单词复制&#xff0…

[ Linux Busybox ] flash_eraseall 命令解析

文章目录 相关结构体flash_eraseall 函数实现flash_eraseall 实现流程图 文件路径&#xff1a;busybox-1.20.2/miscutils/flash_eraseall.c 相关结构体 MTD 相关信息结构体 struct mtd_info_user {__u8 type; // MTD 设备类型__u32 flags; // MTD设…

14.序列化和文件的输入/输出 保存对象

14.1 保存对象状态 你已经奏出完美的乐章&#xff0c;现在会想把它储存起来。你可以抓个文房四宝把它记下来&#xff0c;但也可以按下储存按钮(或按下File菜单上的Save)。然后你帮文件命名&#xff0c;并希望这个文件不会让屏幕变成蓝色的画面。 储存状态的选择有很多种&…

App备案-iOS云管理式证书 Distribution Managed 公钥及证书SHA-1指纹的获取方法

​ 根据近日工业和信息化部发布的《工业和信息化部关于开展移动互联网应用程序备案工作的通知》&#xff0c;相信不少要进行IOS平台App备案的朋友遇到了一个问题&#xff0c;就是apple不提供云管理式证书的下载&#xff0c;也就无法获取公钥及证书SHA-1指纹。 ​ 已经上架的应用…

aosp定制android系统

目录 AOSP 准备工作(配置) 确定机型和版本 初始化 git安装 curl安装 同步源码 环境变量 创建aosp目录 指定同步版本 解下来安装编译需要的依赖 编译aosp源码 刷入系统 AOSP 全称 Android Open Source Project 是指Android开源项目&#xff0c;它是由Google主导的…

【有源码】基于uniapp的农场管理小程序springboot基于微信小程序的农场检测系统(源码 调试 lw 开题报告ppt)

&#x1f495;&#x1f495;作者&#xff1a;计算机源码社 &#x1f495;&#x1f495;个人简介&#xff1a;本人七年开发经验&#xff0c;擅长Java、Python、PHP、.NET、微信小程序、爬虫、大数据等&#xff0c;大家有这一块的问题可以一起交流&#xff01; &#x1f495;&…

算法:分治法-力扣题最大子数组和

文章目录 概念应用步骤实现过程-快速排序为例具体实现步骤&#xff1a;代码实现&#xff1a; 力扣-2586统计范围内的元音字符题解 概念 分治法是一种算法思想&#xff0c;其核心思想是将一个大问题分割成若干个小问题来解决。通过对小问题的分别计算&#xff0c;最终得到大问题…

SEO是什么?独立站如何进行SEO优化

创建一个独立网站并不是难事&#xff0c;但要做好独立网站并进行SEO优化以增加自然流量可能是一个不小的挑战。今天&#xff0c;我们将分享一些关于独立网站SEO优化的技巧&#xff0c;并详细探讨如何提升流量。 在本文中&#xff0c;我们将主要关注谷歌SEO&#xff0c;但请不要…

2000-2022年上市公司专利申请、创新绩效数据

2000-2022年上市公司专利申请、创新绩效数据 1、时间&#xff1a;2000-2022年 2、指标&#xff1a;年份、股票代码、股票简称、行业名称、行业代码、省份、城市、区县、行政区划代码、城市代码、区县代码、首次上市年份、上市状态、专利申请总量、发明专利申请总量、实用新型…

Linux--vim

文章目录 Vim的介绍Vim的几种模式命令模式下的基本操作批量化注释Vim的简单配置使用插件 Vim的介绍 Vim是一个强大的文本编辑器&#xff0c;是从vi编辑器发展而来的&#xff0c;在vi编辑器的基础上进行了改进和拓展&#xff0c;具有强大的特性和功能。 Vim是一个自由开源软件&…

技术分享 | app自动化测试(Android)--显式等待机制

WebDriverWait类解析 WebDriverWait 用法代码 Python 版本 WebDriverWait( driver,timeout,poll_frequency0.5,ignored_exceptionsNone) 参数解析&#xff1a; driver&#xff1a;WebDriver 实例对象 timeout: 最长等待时间&#xff0c;单位秒 poll_frequency: 检测的间…