加载数据时出现错误“ InvalidCastException:指定的强制转换无效”

发布于 2025-01-11 13:26:31 字数 1358 浏览 0 评论 0原文

例如,我想选择一个角色并

private Storage storage;
void Awake()
{
   storage = new Storage();
}
public void SelectChar1()
    {
     numberChar = 1;//byte
     storage.Save(DataPlayerSave);//save works fine
    }

在唤醒时保存他的号码,该号码已加载

private DataPlayerSave dataPlayer;
private byte numberChar;

private void Awake()
      {
        dataPlayer = (DataPlayerSave)storage.Load(new DataPlayerSave());
        numberChar = dataPlayer.numerChar;
      }

我尝试将数据分成几个部分和一个大文件,结果几乎总是相同的(有时一切正常)

public class Storage()
{
    public object Load(object saveDataByDefault)
        {
            filePath = Application.persistentDataPath + "/saves/GameSave.save";
            if (!File.Exists(filePath))
            {
                if (saveDataByDefault != null)
                {
                    Save(saveDataByDefault);
                    return saveDataByDefault;
                }
            }
            var file = File.Open(filePath, FileMode.Open);
    
            var saveData = formatter.Deserialize(file);
            file.Close();
            return saveData;
        }
}

还有类似的类在场景开始时加载数据。如果有 2 个或更多,则给出错误,如果为 1,则一切正常。我尝试使用协程设置序列没有帮助。 加载数据时出现错误“InvalidCastException:指定的转换无效。”

dataPlayer = (DataPlayerSave)storage.Load(new DataPlayerSave());

e.g. I want to select a character and save his number

private Storage storage;
void Awake()
{
   storage = new Storage();
}
public void SelectChar1()
    {
     numberChar = 1;//byte
     storage.Save(DataPlayerSave);//save works fine
    }

on awakening, the number is loaded

private DataPlayerSave dataPlayer;
private byte numberChar;

private void Awake()
      {
        dataPlayer = (DataPlayerSave)storage.Load(new DataPlayerSave());
        numberChar = dataPlayer.numerChar;
      }

I tried to divide the data into several parts and one large file, the result is almost always the same (sometimes everything works)

public class Storage()
{
    public object Load(object saveDataByDefault)
        {
            filePath = Application.persistentDataPath + "/saves/GameSave.save";
            if (!File.Exists(filePath))
            {
                if (saveDataByDefault != null)
                {
                    Save(saveDataByDefault);
                    return saveDataByDefault;
                }
            }
            var file = File.Open(filePath, FileMode.Open);
    
            var saveData = formatter.Deserialize(file);
            file.Close();
            return saveData;
        }
}

There are also similar classes that load data at the beginning of the scene. If there are 2 or more of them, then it gives an error, if 1, then everything works. I tried to set the sequence using the Coroutine did not help.
When loading data it gives an error "InvalidCastException: Specified cast is not valid."

dataPlayer = (DataPlayerSave)storage.Load(new DataPlayerSave());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁以何悠 2025-01-18 13:26:31

如果formatterBinaryFormatter请不要这样做 - 这会伤害你;至于例外情况:从根本上来说,使用调试器并逐步执行代码。特别是,如果您说异常来自:

dataPlayer = (DataPlayerSave)storage.Load(new DataPlayerSave());

那么我们可以假设 Load 返回 DataPlayerSave。那么:它是什么?我们无法告诉您,但是:您可以找出:

var obj = storage.Load(new DataPlayerSave());
var type = obj.GetType(); // put a break-point here
Log(type.FullName); // or just log it
dataPlayer = (DataPlayerSave)obj;

并调查obj到底是什么。请注意,当您更改类型(重命名、移动、重构等)时,BinaryFormatter 非常脆弱 - 但这甚至不是不使用它的首要原因。

if formatter is BinaryFormatter: please don't do that - it will hurt you; as for the exception: fundamentally, use a debugger and step through the code. In particular, if you say that the exception is coming from:

dataPlayer = (DataPlayerSave)storage.Load(new DataPlayerSave());

then we can assume that Load is not returning a DataPlayerSave. So: what is it? We can't tell you, but: you can find out:

var obj = storage.Load(new DataPlayerSave());
var type = obj.GetType(); // put a break-point here
Log(type.FullName); // or just log it
dataPlayer = (DataPlayerSave)obj;

and investigate what exactly obj is. Note that BinaryFormatter is very brittle as you change types (rename, move, refactor, etc) - but that isn't even the top reason not to use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文