C# 二进制序列化后字符串为空
我正在制作一个简单的游戏,其中一个功能是挑战,它将从文件中打开,下面是其中一个挑战的外观:C# 二进制序列化后字符串为空,c#,unity3d,C#,Unity3d,我正在制作一个简单的游戏,其中一个功能是挑战,它将从文件中打开,下面是其中一个挑战的外观: [System.Serializable] public class Challenge10 : ChallegeInterface { public bool isCompleted = false; public string description = "test"; public int curCoins; public override void giveRewa
[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 openChallenges(){
挑战=新挑战界面[game_data.challenges.Length];
对于(int i=0;i
除了描述之外,其他一切似乎都很好。我错过什么了吗
编辑:
以下是程序序列化对象的方式:
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 void save(T数据,字符串文件名),其中T:class{
如果(文件名==“”)
Log(“空文件路径”);
FileStream file=null;
试一试{
如果(fileName.IndexOf(“/”)大于0){
字符串[]strDirName=fileName.Split(新字符[]{'/'});
字符串dirName=strDirName[0];
如果(!Directory.Exists(Path.Combine(Application.persistentDataPath,dirName))){
CreateDirectory(Path.Combine(Application.persistentDataPath,dirName));
}
}
file=file.Create(Path.Combine(Application.persistentDataPath,fileName));
序列化(文件、数据);
Log(“成功保存文件”+文件名);
}捕获(IOE异常){
Log(例如ToString());
}最后{
如果(文件!=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 ();
}
}
}
publictload(字符串文件名),其中T:class{//T=>对象类型
如果(文件名==null){
Log(“文件的空路径”);
返回null;
}否则{
FileStream file=null;
试一试{
//打开文件;
file=file.Open(constructFilePath(fileName),FileMode.Open);
//删除
Log(“成功加载文件”);
//反序列化打开的文件,将其强制转换为T并返回
返回binFormatter.Deserialize(文件)为T;
}捕获(IOE异常){
//删除
Log(例如ToString());
//保存当前对象以防文件不存在
//使用Activator创建对象的实例,
保存(Activator.CreateInstance(),文件名);
//再次调用该函数
返回加载(文件名);
}最后{
//关闭文件
如果(文件!=null)
Close();
}
}
}
通过检查openChallenges()
方法中的以下行:
ChallegeInterface challenge = HddManager.HddManagerInstance.load<ChallegeInterface> ("challenges/challenge"+curChallenge);
Debug.Log (challenge.description);
ChallegeInterface challenge=HddManager.HddManagerInstance.load(“挑战/挑战”+curChallenge);
Debug.Log(challenge.description);
我假设ChallegeInterface
实际上是一个类而不是一个接口,因为接口不能包含字段,并且您正在访问质询。说明
当质询
是ChallegeInterface
这意味着ChallengeInterface
是一个类
在这种情况下,您实际上访问的是基类(ChallegeInterface
)字段,而不是正确的(Challenge10
)。这个字段是空的
重要提示:保持清晰正确的编码约定,不要命名类接口,最好避免使用编程术语命名类型,而不要使用与其用法相关的指示性命名
备注:我自己检查了序列化,并检查了
Challenge10
的描述,它工作正常。您如何序列化/取消对对象的序列化?尝试改用xml序列化吗?我编辑了这个问题。作为最后一种手段,我将重写所有使用XML的代码,但如果有可能修复此代码,它将非常棒。奇怪的是,它对我来说很好,尝试在ChallegeInterface challenge=HddManager.HddManagerInstance.load(“challenges/challenge”+curChallenge)之后进行调试代码>行并手动检查说明。它仍然是空的吗?我认为问题可能是您正在尝试序列化接口。还有更多。等等,你是怎么做到的?:ChallegeInterface challenge=HddManager.HddManagerInstance.load(“challenges/challenge”+curChallenge);调试日志(challenge.description)代码>接口不能包含字段。这是一节课吗?如果是这样,您正在检查Debug.Log(challenge.description)
中的基类字段,该字段为空。。。