【发布时间】:2014-11-23 21:41:15
【问题描述】:
我正在努力从两个单独的脚本中保存/读取文件,第一次测试尝试在 Unity 中的 playerPrefs 之外保存/读取文件。我将文件保存在一个场景中,并尝试在另一个场景中打开文件。显然有演员表问题,但我无法弄清楚问题是什么。
这是创建文件的代码:
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SubmitScript : MainScript {
[Serializable]
public class CardEntry {
public int card_nr;
public bool card_backFaceUp;
public float card_posX;
public float card_posY;
public float card_posZ;
public float card_rotZ;
}
public List<CardEntry> cardsList = new List<CardEntry>();
private GameObject cards_gameObject;
public void ReturnToSetupTemplatePanel () {
// Set playerPref so the GameBoard is back to normal
PlayerPrefs.SetString ("GameBoard Type","DoNormal");
// Set playerPrefs so the Template panel is out
PlayerPrefs.SetString ("PanelPosition", "TemplatePanel Visable");
// Set info that there is an updated template file
PlayerPrefs.SetString ("TemplateFile", "Exists");
// Process the save operation
CardEntry _cardEntry = new CardEntry ();
_cardEntry.card_nr = 0;
// Save all card objects properties
foreach (GameObject aGO in master_GameObject_List) {
cards_gameObject = GameObject.FindWithTag(aGO.tag);
// Add the cards properties
_cardEntry.card_nr++;
if (cards_gameObject.tag.LastIndexOf ("B") != -1) { // Card have back face up
_cardEntry.card_backFaceUp = false;
}
else {
_cardEntry.card_backFaceUp = true;
}
_cardEntry.card_posX = aGO.transform.position.x;
_cardEntry.card_posY = aGO.transform.position.y;
_cardEntry.card_posZ = aGO.transform.position.z;
_cardEntry.card_rotZ = aGO.transform.rotation.z;
cardsList.Add(_cardEntry);
print ("%: " + _cardEntry.card_nr + " % " + aGO.tag);
}
// Save card object data
// Get binary formatter
var b = new BinaryFormatter ();
// Create a file
var f = File.Create (Application.persistentDataPath + "/tempInfo.dat");
// Save the scores
b.Serialize (f, cardsList);
f.Close ();
// Go back to SetupScene
Application.LoadLevel("SetupScene");
}
}
这是我用来打开文件并尝试处理输出的代码:
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class StartupScript : MonoBehaviour {
[Serializable]
public class CardEntry {
public int card_nr;
public bool card_backFaceUp;
public float card_posX;
public float card_posY;
public float card_posZ;
public float card_rotZ;
}
public List<CardEntry> cardsList2 = new List<CardEntry>();
private bool shouldPanelBeOut = false;
private string panelStatus;
public GameObject thePanel;
private string fileStatus;
public GameObject cards_gameObject;
void Start() {
print ("Startup Script");
// If needed
//File.Delete (Application.persistentDataPath + "/tempInfo.dat");
CardEntry _cardEntry = new CardEntry ();
// Find out if the panel should be visable or not
panelStatus = PlayerPrefs.GetString ("PanelPosition");// "TemplatePanel Visable"
fileStatus = PlayerPrefs.GetString ("TemplateFile");
if (panelStatus == "TemplatePanel Visable") {
thePanel.GetComponent<RectTransform>().localPosition = new Vector3(0.0f, 0.0f, 0.0f);
if (fileStatus == "Exists") {
// If not blank then load it
if (File.Exists (Application.persistentDataPath + "/tempInfo.dat")) {
print ("File Exist");
// Binary formatter for loading back
var b = new BinaryFormatter();
// Get the file
var f = File.Open(Application.persistentDataPath + "/tempInfo.dat", FileMode.Open);
// Load back the scores
cardsList2 = (List<CardEntry>)b.Deserialize(f); //<<< CAST ERROR HERE
f.Close();
}
}
else {
print("File does not Exist");
}
}
print ("cardsList2.Count: " + cardsList2.Count);
}
}
转换错误在这一行:
cardsList2 = (List<CardEntry>)b.Deserialize(f); //<<< CAST ERROR HERE
...文件也存在
我已经测试了几个小时,但无法弄清楚问题所在。
【问题讨论】:
-
发布原始异常详细信息(异常类型、消息、堆栈跟踪)而不是 CAST ERROR HERE,它没有说明问题。
-
在您的可序列化类中,您是否考虑过使用自动 getter 和 setter..?
-
可序列化列表中的所有 CardEntries 都将重复。您只生成一个新的 CardEntry() 并更改其值。在 C# 中,一切都是参考 :)
-
首先,为你的序列化类添加一个无参数构造函数。接下来,显示一条错误消息。