Giter Club home page Giter Club logo

unityjsonhelper's Introduction

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
}

unityjsonhelper's People

Contributors

bekwnn avatar guneyozsan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.