1

嗨,我在统一中使用了一个脚本对象,它在我的游戏中保存了商店的数据,它现在已经工作了一段时间了,但是当我今天在更新统一集线器后打开统一时,我的代码中出现了这个错误

ArgumentException:JSON 解析错误:文档为空。UnityEngine.JsonUtility.FromJsonOverwrite (System.String json, System.Object objectToOverwrite) (在 <1386288601af43018501cce2912f52f4>:0)

它在我在 ScriptableObjec 对象代码中引用 LoadDataFromFile 函数的任何地方都出现了错误。错误出现在我的代码的第 50 行。感谢您的时间

这是 ScriptableObjects 代码

using System.Collections.Concurrent;
using System.ComponentModel.Design;
using UnityEngine;
using System.IO;
using System;
[CreateAssetMenu(fileName = "data",menuName = "sss",order = 1)]

public class sss : ScriptableObject
{
public float coins = 0f;
public float speed = 10f;
public float jump = 25f;
public bool buyspeed = false;
public bool buyjump = false;
public bool shotgununlock = false;
public bool set = true;
public int face = 1;
public int gun = 1;
public bool sniperunlock = false;


private const string FILENAME = "sss.dat";

public void SaveToFile()
{
    var filePath = Path.Combine(Application.persistentDataPath, FILENAME);

    if (!File.Exists(filePath))
    {
        File.Create(filePath);
    }

    var json = JsonUtility.ToJson(this);
    File.WriteAllText(filePath, json);
}


public void LoadDataFromFile()
{
    var filePath = Path.Combine(Application.persistentDataPath, FILENAME);

    if (!File.Exists(filePath))
    {
        //Debug.LogWarning($"File /"{ filePath}/ " not found!, this);
        return;
    }

    var json = File.ReadAllText(filePath);
    JsonUtility.FromJsonOverwrite(json, this); //<-----this line of code
}
}
4

1 回答 1

0

我最终通过重命名 JSON 文件并用它替换这行代码来修复private const string FILENAME = "sss.dat";private const string FILENAME = "NEW FILE NAME";

于 2020-06-14T18:04:36.797 回答