c# - Unity 无法打开二进制文件

标签 c# unity-game-engine

这就是我在开发套件中保存关卡数据的方式。(这在开发程序中运行)
并且在开发套件中也能够正确恢复数据。

public void Savedata()
{
    List<List<float>> tempfloatlist = new List<List<float>>();

    foreach (List<Vector2> ele in routes)
    {
        conversions.Vec2float temp = new conversions.Vec2float();
        tempfloatlist.Add(temp.conv2float(ele));
    }

    BinaryFormatter binform = new BinaryFormatter();
    FileStream savefile = File.Create(Application.persistentDataPath + 
    "/DevData.bytes");

    DevData savecontainer = new DevData();
    savecontainer.routenames = routenames;
    savecontainer.routes = tempfloatlist;
    savecontainer.waves = waves;

    binform.Serialize(savefile, savecontainer);
    savefile.Close();
}

这就是我在将文件移动到资源中后尝试打开数据的方式。(这在实际游戏中运行)请参阅行\ERROR

NullReferenceException: Object reference not set to an instance of an object GameControl.LoadLevelData () (at Assets/GameControl.cs:70) GameControl.Awake () (at Assets/GameControl.cs:26)

我担心我没有以正确的方式打开文件。

private void LoadLevelData()
{

    TextAsset devdataraw = Resources.Load("DevData") as TextAsset;
    BinaryFormatter binform = new BinaryFormatter();
    Stream loadfile = new MemoryStream(devdataraw.bytes);
    DevData devdata = binform.Deserialize(loadfile) as DevData;
    \\ERROR happens here, no correct data to be loaded in routenames.        
    routenames = devdata.routenames;
    waves = devdata.waves;
    routes = new List<List<Vector2>>();
    foreach (List<float> ele in devdata.routes)
    {
        conversions.float2vec temp = new conversions.float2vec();
        routes.Add(temp.conv2vec(ele));
    }
    loadfile.Close();
}

 [Serializable()]
 class DevData
 {
   public List<List<float>> routes;
   public List<string> routenames;
   public List<Wave> waves;
 }
namespace WaveStructures
{
[Serializable()]
public class Enemy
{
    public int enemytype;
    public int movementpattern;
    public int maxhealth;
    public int speed;
}
[Serializable()]
public class Spawntimer
{
    public float timer;
    public int amount;       
}

[Serializable()]
public class Wave

{
    public List<Enemy> Enemylist;
    public List<Spawntimer> Enemyspawnsequence;
    public int[] enemypool;
}
}

最佳答案

序列化器很难序列化数据。

只有两种可能的解决方案可供尝试:

1。请注意 [Serialized()] 中的 ()。删除它。那应该是[Serialized]另一个 user提及这是有效的。确保执行#2。

2。确保要序列化的每个类都放置在其自己的文件中。确保它也不继承自 MonoBehaviour

例如,DevData 类应位于其自己的名为 DevData.cs 的文件中。您还应该对要序列化的 Wave 和其他类执行此操作。


最后,如果这不能解决您的问题,那么这是一个已知问题,即 BinaryFormatter 在 Unity 中使用时会导致如此多的问题。你应该放弃它并使用 Json 代替。看看this帖子描述了如何使用 Json 来实现此目的。

关于c# - Unity 无法打开二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45476596/

相关文章:

c# - 为什么 IList<T> 不只从 ICollection<T> 继承?

c# - C#新手,找不到数据表

c# - 什么构成 'redundant delegate creation' ?

c# - 如何确定分布式架构?

c# - 无法使用 Unity 在 Firebase-Analytics 中获取自定义参数

c# - "Read .JSON file"统一 C# 中的方法,也适用于 IOS 和 Android 平台

c# - 在 asp.net 中传递数据列表值(已关闭)

android - Unity 的 Facebook 插件 (v5.0.2 BETA) 缺少 bin 文件夹

c# - 沿 3d 向量查找点

unity-game-engine - 了解相机,使其适合 480x800