【发布时间】:2015-09-20 18:31:20
【问题描述】:
我正在制作一个简单的游戏,其中一个功能是将从文件中打开的挑战,这是其中一个挑战的外观:
[System.Serializable]
public class Challenge10 : ChallegeInterface {
public bool isCompleted = false;
public string description = "test";
public int curCoins;
public override void giveReward (){
GameDataSingleton.gameDataInstance.game_data.addCoinsFromReward (7);
}
//Method to check if everything is completed
public override bool isComplete (){
if (!isCompleted) {
curCoins= GameDataSingleton.gameDataInstance.game_data.getCoinsThisGame ();
if (curCoins >= 1) {
isCompleted = true;
giveReward ();
return true;
}
}
return false;
}
}
问题是在我反序列化文件后,字符串值(描述)为空。这是程序打开挑战的代码。
public void openChallenges(){
challenges = new ChallegeInterface[game_data.challenges.Length];
for (int i = 0; i < game_data.challenges.Length; i++) {
int curChallenge = game_data.challenges[i];
ChallegeInterface challenge = HddManager.HddManagerInstance.load<ChallegeInterface> ("challenges/challenge"+curChallenge);
Debug.Log (challenge.description);
challenges[i] = challenge;
}
}
除了描述之外,其他一切似乎都很好。我错过了什么吗?
编辑: 这是程序序列化对象的方式:
public void save<T>(T data, string fileName) where T : class{
if (fileName == "")
Debug.Log ("Empty file path");
FileStream file = null;
try{
if(fileName.IndexOf("/") > 0){
string[] strDirName = fileName.Split(new char[] {'/'});
string dirName = strDirName[0];
if(!Directory.Exists(Path.Combine(Application.persistentDataPath, dirName))){
Directory.CreateDirectory(Path.Combine(Application.persistentDataPath, dirName));
}
}
file = File.Create(Path.Combine(Application.persistentDataPath, fileName));
binFormatter.Serialize(file, data);
Debug.Log ("File saved succesfully" + fileName);
}catch(IOException e){
Debug.Log(e.ToString());
}finally{
if(file != null)
file.Close();
}
}
这是反序列化对象的方式:
public T load<T> (string fileName) where T : class{ // T => object type
if (fileName == null) {
Debug.Log ("Empty path to file");
return null;
} else {
FileStream file = null;
try {
//Open the file;
file = File.Open (constructFilePath(fileName), FileMode.Open);
//To be removed
Debug.Log ("File loaded succesfully");
// Deserialize the opened file, cast it to T and return it
return binFormatter.Deserialize (file) as T;
} catch (IOException e) {
//To be removed
Debug.Log (e.ToString ());
// Saves the current object in case the file doesn't exist
// Use Activator to create instance of the object,
save (Activator.CreateInstance<T> (), fileName);
// calls the function again
return load<T> (fileName);
} finally {
//Close the file
if (file != null)
file.Close ();
}
}
}
【问题讨论】:
-
你是如何序列化/去消毒你的对象的?改用xml序列化试试?
-
我编辑了这个问题。作为最后的手段,我将重写一切以使用 XML,但如果可以修复此代码,那就太好了。
-
奇怪,它对我来说很好,尝试在
ChallegeInterface challenge = HddManager.HddManagerInstance.load<ChallegeInterface> ("challenges/challenge"+curChallenge);行之后调试并手动检查描述。还是空的吗? -
我认为问题可能在于您正在尝试序列化接口。这里有更多info。
-
等等,你是怎么做到的?:
ChallegeInterface challenge = HddManager.HddManagerInstance.load<ChallegeInterface> ("challenges/challenge"+curChallenge); Debug.Log(challenge.description);接口不能包含字段。是一门课吗?如果是这样,您正在检查Debug.Log(challenge.description)中为空的基类字段...