为什么LitJSON不会在构建中工作,但在编辑器中有效吗?

时间:2017-03-09 03:08:05

标签: json user-interface unity3d build litjson

我在Unity工作,我正在尝试将LitJSON用于库存和能力系统,以及统计数据。我做了那个工作,这一切都通过我放在脚本中的一些功能起作用。团结一致,效果很好。但是,当我为pc standalone构建游戏时,来自JSON文件的所有数据都不起作用,并且UI显示空白文本。

这是我的角色统计控制功能脚本:

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;

        }
    }
}

此脚本在整个游戏中使用,并且统计信息的值显示在屏幕上: enter image description here

然而,这就是我实际构建它时的样子:enter image description here

1 个答案:

答案 0 :(得分:1)

您可以查找代码。

  • 首先尝试使用Resource.Load TextAsset;

TextAsset

Resources.Load

  • 第二次尝试使用JSONObject或Minijson加载所有属性并在一个类中实现:

JSONobject

MiniJSON