加载数据时出现错误“ InvalidCastException:指定的强制转换无效”
例如,我想选择一个角色并
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
formatter
是BinaryFormatter
:请不要这样做 - 这会伤害你;至于例外情况:从根本上来说,使用调试器并逐步执行代码。特别是,如果您说异常来自:那么我们可以假设
Load
不返回DataPlayerSave
。那么:它是什么?我们无法告诉您,但是:您可以找出:并调查
obj
到底是什么。请注意,当您更改类型(重命名、移动、重构等)时,BinaryFormatter
非常脆弱 - 但这甚至不是不使用它的首要原因。if
formatter
isBinaryFormatter
: 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:then we can assume that
Load
is not returning aDataPlayerSave
. So: what is it? We can't tell you, but: you can find out:and investigate what exactly
obj
is. Note thatBinaryFormatter
is very brittle as you change types (rename, move, refactor, etc) - but that isn't even the top reason not to use it.