为什么 LitJSON 在构建中不起作用,但在编辑器中起作用?

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

我在 Unity 工作,我正在尝试将 LitJSON 用于库存和能力系统以及统计数据。我完成了这项工作,这一切都是通过我在脚本中放入的一些功能来完成的。在统一中,它工作得很好。但是,当我为 PC 独立构建游戏时,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;

        }
    }
}

整个游戏都使用了这个脚本,屏幕上显示了统计值:

然而,这是我实际构建时的样子:

您可以查看您的代码。

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

TextAsset

Resources.Load

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

JSONobject

MiniJSON