UnityJsonHelper
A simple utility class to help you use Unity 5.3's JsonUtility better.
Install / Use
/learn @Bekwnn/UnityJsonHelperREADME
UnityJsonHelper
JsonHelper is a small, simple utility class designed to allow you to grab a json object (or objects) that match the provided key. The reason for this class, is that you probably want to have json files with several objects worth of data, but JsonUtility doesn't provide any way of handling that.
Some example use:
Hero.json
{
"pyroKid": {
"attack": {
"damage": 50,
"reload": 1.8
},
"defense": {
"health": 120,
"regen": 5
}
},
"iceKid": {
"attack": {
"damage": 20,
"reload": 0.6
},
"defense": {
"health": 170,
"regen": 8
}
}
SomeUnityCode.cs
using UnityEngine;
using System;
using System.IO;
// look up JsonUtility in the Unity api if this bit confuses you
[Serializable]
public class AttackInfo
{
public float damage;
public float reload;
}
// . . .
public void SomeGameFunction()
{
// gets the pyro kid json string
string pyroKidJson = JsonHelper.GetJsonObject(File.ReadAllText(jsonFilePath), "pyroKid");
// gets pyro kid's attack component
string pyroAttackJson = JsonHelper.GetJsonObject(pyroKidJson, "attack");
// and then we get our attack component as an object!
AttackInfo info = JsonUtility.FromJson<AttackInfo>(pryoAttackJson);
// doing things piece-wise like this is kinda like navigating through the json file
// I may add a better way of doing this in the future, ex GetJsonObjectFromPath(jsonStr, "pyroKid/attack")
//alternatively, if we want all the attack components
List<AttackInfo> attackInfos = new List<AttackInfo>();
//gets array of json string objects
string[] allAttackJsons = JsonHelper.GetJsonObjects(File.ReadAllText(jsonFilePath), "attack");
foreach (string jsonObj in allAttackJsons)
{
attackInfos.Add(JsonUtility.FromJson<AttackInfo>(jsonObj));
}
// and now we have a list of all attack components in the json file!
// Not very useful in this hypothetical example,
// but useful in other situations like loading save data
}
Related Skills
node-connect
351.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
