JsonUtility.FromJsonOverwrite错误unity。

问题描述 投票:0回答:1

嗨,我在unity中使用一个scriptibleobject,在我的游戏中保存数据的商店,它一直在正常工作,现在一段时间,但当我打开unity今天更新unity hub后,我在我的代码中得到这个错误。

ArgumentException: JSON解析错误。UnityEngine.JsonUtility.FromJsonOverwrite (System.String json, System.ObjectObjectToOverwrite) (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 unity3d scriptable-object
1个回答
0
投票

我最终通过重命名JSON文件和替换这行代码来解决它。private const string FILENAME = "sss.dat"; 以此 private const string FILENAME = "NEW FILE NAME";

© www.soinside.com 2019 - 2024. All rights reserved.