将对象转换为 class C#
cast object to class C#
正在尝试将 .dat 文件转换为它自己的 Class
level0 = (Level0) LoadObjInBinary(level0, "Level" + levelNumber);
public static object LoadObjInBinary(object myClass, string fileName) {
fileName += ".dat";
if (File.Exists(FileLocation + fileName)) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(FileLocation + fileName, FileMode.Open);
myClass = bf.Deserialize(file);
file.Close();
return myClass;
} else {
return null;
}
}
Level() class
[Serializable]
public class Level0 { //Using this class to create Level.dat binary file
static int level = 1;
static int moves = 15;
static int seconds;
static int minScoreForOneStar = 1000;
static int minScoreForTwoStars = 1500;
static int minScoreForThreeStars = 2000;
static TargetObj[] targetObjs = {new TargetObj(Targets.Black, 10), new TargetObj(Targets.Freezer, 1), new TargetObj(Targets.Anchor, 2)};
static Color[] colors = {Constants.grey, Constants.green, Constants.pink, Constants.brown, Constants.purple, Constants.lightBlue};
static Cell[,] levelDesign;
//the rest is Properties of Fields
}
问题:LoadObjInBinary 返回空值。文件路径正确,class也匹配,但不知道为什么“(Level0)对象”不起作用...
谢谢
感谢您提供 Level0 class。
问题是 static 字段永远不会序列化,因为它们不属于您实例化的对象的实例,它们是全局的。
因为我假设你需要它们是静态的,所以它们可以从你的应用程序的所有部分访问,快速 work-around 是创建另一个 class 与 non-static 成员,然后 serialize-deserialize 它,并将它的值分配给 Level0 的全局静态实例(无论你在哪里使用它)。
[Serializable]
class Level0Data
{
int level = 1;
int moves = 15;
int seconds;
int minScoreForOneStar = 1000;
...
}
然后在序列化和反序列化之后你可以做下面的事情。
Level0Data deserializedObject = (Level0Data) LoadObjInBinary(..);
Level0.level = deserializedObject.level;
Level0.moves = deserializedObject.moves;
因为您必须确保 Level0.level,移动和所有其他成员 public,或至少 public 可以通过其他方式进行修改。
此外,您还必须确保
class TargetObj{}
class Cell{}
也被标记为可序列化,否则它们将不会写入文件,也不会写入任何关于它们的反序列化信息。
编辑
在这里你可以找到Unity默认支持的所有可序列化类型:
正在尝试将 .dat 文件转换为它自己的 Class
level0 = (Level0) LoadObjInBinary(level0, "Level" + levelNumber);
public static object LoadObjInBinary(object myClass, string fileName) {
fileName += ".dat";
if (File.Exists(FileLocation + fileName)) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(FileLocation + fileName, FileMode.Open);
myClass = bf.Deserialize(file);
file.Close();
return myClass;
} else {
return null;
}
}
Level() class
[Serializable]
public class Level0 { //Using this class to create Level.dat binary file
static int level = 1;
static int moves = 15;
static int seconds;
static int minScoreForOneStar = 1000;
static int minScoreForTwoStars = 1500;
static int minScoreForThreeStars = 2000;
static TargetObj[] targetObjs = {new TargetObj(Targets.Black, 10), new TargetObj(Targets.Freezer, 1), new TargetObj(Targets.Anchor, 2)};
static Color[] colors = {Constants.grey, Constants.green, Constants.pink, Constants.brown, Constants.purple, Constants.lightBlue};
static Cell[,] levelDesign;
//the rest is Properties of Fields
}
问题:LoadObjInBinary 返回空值。文件路径正确,class也匹配,但不知道为什么“(Level0)对象”不起作用...
谢谢
感谢您提供 Level0 class。
问题是 static 字段永远不会序列化,因为它们不属于您实例化的对象的实例,它们是全局的。
因为我假设你需要它们是静态的,所以它们可以从你的应用程序的所有部分访问,快速 work-around 是创建另一个 class 与 non-static 成员,然后 serialize-deserialize 它,并将它的值分配给 Level0 的全局静态实例(无论你在哪里使用它)。
[Serializable]
class Level0Data
{
int level = 1;
int moves = 15;
int seconds;
int minScoreForOneStar = 1000;
...
}
然后在序列化和反序列化之后你可以做下面的事情。
Level0Data deserializedObject = (Level0Data) LoadObjInBinary(..);
Level0.level = deserializedObject.level;
Level0.moves = deserializedObject.moves;
因为您必须确保 Level0.level,移动和所有其他成员 public,或至少 public 可以通过其他方式进行修改。
此外,您还必须确保
class TargetObj{}
class Cell{}
也被标记为可序列化,否则它们将不会写入文件,也不会写入任何关于它们的反序列化信息。
编辑
在这里你可以找到Unity默认支持的所有可序列化类型: