JsonUtility.FromJsonOverwrite error unity



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

参数异常:JSON 解析错误:文档为空。 UnityEngine.JsonUtility.FromJsonOverwrite (System.String json, System.Object objectToOverwrite( (at <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
}
}

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

最新更新