Search code examples
jsonuser-interfaceunity-game-enginebuildlitjson

Why wont LitJSON work in build, but does work in the editor?


I am working in Unity, and I am trying to use LitJSON for an inventory and ability system, as well as stats. I made that work, and it all works via some functions I put in a script. In unity, it works really well. However, when I build the game for pc standalone, all of the data from the JSON files doesn't work, and the UI shows blank text.

Here is my Character Stat Control functions script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using LitJson;
using System.Linq;


public class Character
{
    public string Name      { get; set; }
    public int Id           { get; set; }
    public int Strength     { get; set; }
    public int Intelligence { get; set; }
    public int Defense      { get; set; }
    public int Resistance   { get; set; }
    public int Accuracy     { get; set; }
    public int Agility      { get; set; }
    public int Speed        { get; set; }
    public int MaxAP        { get; set; }
    public int CurrentAP    { get; set; }
    public int MaxHP        { get; set; }
    public int CurrentHP    { get; set; }
    public int Affinity     { get; set; }

    public Character() {

    }
}

public class CharacterStats : MonoBehaviour
{
    public static string character1FilePath = "/Resources/character1Stats.json";
    public static string character2FilePath = "/Resources/character2Stats.json";
    public static string character3FilePath = "/Resources/character3Stats.json";
    public static string character4FilePath = "/Resources/character4Stats.json";


    public static string GetName (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Name;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Name;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Name;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Name;

        default:
            return null;

        }
    }

    public static int GetStrength (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Strength;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Strength;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Strength;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Strength;

        default:
            return 0;

        }
    }

    public static int GetIntelligence (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Intelligence;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Intelligence;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Intelligence;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Intelligence;

        default:
            return 0;

        }
    }


    public static int GetDefense (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Defense;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Defense;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Defense;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Defense;

        default:
            return 0;

        }
    }


    public static int GetResistance (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Resistance;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Resistance;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Resistance;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Resistance;

        default:
            return 0;

        }
    }


    public static int GetAccuracy (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Accuracy;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Accuracy;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Accuracy;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Accuracy;

        default:
            return 0;

        }
    }

    public static int GetAgility (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Agility;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Agility;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Agility;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Agility;

        default:
            return 0;

        }
    }

    public static int GetSpeed (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Speed;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Speed;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Speed;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Speed;

        default:
            return 0;

        }
    }

    public static int GetMaxAP (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.MaxAP;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.MaxAP;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.MaxAP;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.MaxAP;

        default:
            return 0;

        }
    }

    public static int GetCurrentAP (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.CurrentAP;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.CurrentAP;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.CurrentAP;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.CurrentAP;

        default:
            return 0;

        }
    }

    public static int GetMaxHP (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.MaxHP;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.MaxHP;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.MaxHP;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.MaxHP;

        default:
            return 0;

        }
    }

    public static int GetCurrentHP (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.CurrentHP;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.CurrentHP;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.CurrentHP;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.CurrentHP;

        default:
            return 0;

        }
    }

    public static int GetAffinity (int characterID) {
        Character character = new Character ();
        string characterString;
        switch (characterID) {
        case 1:
            characterString = File.ReadAllText (Application.dataPath + character1FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Affinity;

        case 2:
            characterString = File.ReadAllText(Application.dataPath + character2FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Affinity;

        case 3:
            characterString = File.ReadAllText(Application.dataPath + character3FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Affinity;

        case 4:
            characterString = File.ReadAllText(Application.dataPath + character4FilePath);
            character = JsonMapper.ToObject<Character> (characterString);
            return character.Affinity;

        default:
            return 0;

        }
    }
}

This script is used all over the game, and the values of the stats are displayed on the screen: enter image description here

However, this is what it looks like when I actually build it:enter image description here


Solution

  • You can look up your code.

    • First try to use Resource.Load TextAsset ;

    TextAsset

    Resources.Load

    • Second try use JSONObject or Minijson to load all properties and implement in one class :

    JSONobject

    MiniJSON