Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法从资源中加载文件,然后通过BinaryFormatter对其进行反序列化?_C#_Unity3d - Fatal编程技术网

C# 有没有办法从资源中加载文件,然后通过BinaryFormatter对其进行反序列化?

C# 有没有办法从资源中加载文件,然后通过BinaryFormatter对其进行反序列化?,c#,unity3d,C#,Unity3d,我目前正在尝试制作一个系统来保存和加载场景中没有的级别。我对当前的系统感到满意,它看起来是这样的: string path = Application.dataPath + "/Levels/" + name + ".lvl"; if (File.Exists (path)) { BinaryFormatter formatter = new BinaryFormatter (); FileStream stream

我目前正在尝试制作一个系统来保存和加载场景中没有的级别。我对当前的系统感到满意,它看起来是这样的:

        string path = Application.dataPath + "/Levels/" + name + ".lvl";
        if (File.Exists (path)) {
            BinaryFormatter formatter = new BinaryFormatter ();
            FileStream stream = new FileStream (path, FileMode.Open);
            LevelInformation data = formatter.Deserialize (stream) as LevelInformation;
            stream.Close ();
            return data;
        } else {
            Debug.LogError ("No level found at " + path);
            return null;
        }
    }
但是,我希望它能让玩家看不到和调整任何官方和非自定义的关卡,所以我决定将官方关卡放在资产中,并通过Resources.load()加载它们。就像这样:

    public static LevelInformation LoadLevelOfficial (string name) {
        Object levelFile = Resources.Load("/Levels/" + name + ".lvl");
        if (levelFile) {
            BinaryFormatter formatter = new BinaryFormatter ();
            FileStream stream = new FileStream (levelFile, FileMode.Open);
            LevelInformation data = formatter.Deserialize (stream) as LevelInformation;
            stream.Close ();
            return data;
        } else {
            Debug.LogError ("No official level called " + name + "can be found in the game files!");
            return null;
        }
    }

然而,Unity告诉我不能使用对象变量levelFile,而是希望我使用一些IntPtr。有没有办法将其转换为IntPtr,或者如何反序列化从资源加载的对象?

您可以将文件加载为,并使用例如

TextAsset levelFile = Resources.Load<TextAsset>("/Levels/" + name + ".lvl");
using (var stream = new MemoryStream(levelFile.bytes))
{
    var formatter = new BinaryFormatter ();
    var data = (LevelInformation)formatter.Deserialize (stream);
    return data;
}
TextAsset levelFile=Resources.Load(“/Levels/”+name+”.lvl”);
使用(var stream=newmemoryStream(levelFile.bytes))
{
var formatter=newbinaryformatter();
var data=(LevelInformation)formatter.Deserialize(流);
返回数据;
}
注意但是:

如果使用文本资源包含二进制数据,则应确保文件具有
.bytes
扩展名。对于任何其他扩展名,如果无法将文件解析为utf8字符串,TextImporter将尝试去除非ASCI字符


你也可以分享你的错误日志吗?当然可以
Assets/Resources/Scripts/SaveSys/SaveSystem.cs(100,40):错误CS1503:参数“#1”无法将“UnityEngine.Object”表达式转换为类型“System.IntPtr”
谢谢您的回答。似乎Resources.Load()根本无法用于加载自定义扩展名,如文档所示:“如果path处的文件类型无法转换为t,则还将返回null”。有.bytes扩展名会有帮助吗?是的,我必须使用将级别重命名为.bytes扩展名,并删除Resources.Load()处的第一个正斜杠和扩展名:
TextAsset levelFile=Resources.Load(“levels/”+name)它现在可以工作了。谢谢你的提示!您可以将答案标记为所选答案以帮助其他人。