反序列化文件时,无法从源类型转换为目标类型

时间:2014-09-16 21:33:00

标签: c# unity3d

我正在努力使用两个单独的脚本中的保存/读取文件,第一次在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

...还有文件存在

我已经测试了几个小时,但无法弄清问题是什么。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我以一种简单的方式解决了这个问题。

cardsList2 = b.Deserialize(f) as List<CardEntry>;

我认为,这将解决您的问题