SerializationException和IOException:共享路径冲突

时间:2019-03-29 20:46:25

标签: c# unity3d

尝试为我的游戏创建一个保存和加载系统时,出现以下两个错误。

  

“ SerializationException:程序集UnityEngine.CoreModule中的UnityEngine.MonoBehaviour类型,版本= 0.0.0.0,Culture = neutral,PublicKeyToken = null未标记为可序列化。   System.Runtime.Serialization.FormatterServices.GetSerializableMembers(System.Type类型,StreamingContext上下文)(位于/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization/FormatterServices.cs:101)   System.Runtime.Serialization.Formatters.Binary.CodeGenerator.GenerateMetadataTypeInternal(System.Type类型,StreamingContext上下文)(位于/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary /CodeGenerator.cs:78)   System.Runtime.Serialization.Formatters.Binary.CodeGenerator.GenerateMetadataType(System.Type类型,StreamingContext上下文)(在

  

IOException:在路径上共享冲突       C:\ Users \ Sam \ AppData \ LocalLow \ DefaultCompany \ Game \ test.txt       System.IO.FileStream..ctor(System.String路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,布尔匿名,FileOptions选项)(位于/ Users / builduser / buildslave / mono / build / mcs / class / corlib /System.IO/FileStream.cs:320)       System.IO.FileStream..ctor(System.String路径,FileMode模式)       (包装器远程调用检查)System.IO.FileStream:.ctor(字符串,System.IO.FileMode)       SaveSystem.SavePlayer(.Player播放器)(在Assets / Scripts / SaveSystem.cs:22处)       Player.SavePlayer()(在Assets / Scripts / Player.cs:47处)       Player.Update()(位于Assets / Scripts / Player.cs:13)

这是保存和加载方法 下面的Savesystem类

public static class SaveSystem {
public static void SavePlayer (Player player){
    BinaryFormatter formatter = new BinaryFormatter();
    string path = Application.persistentDataPath + "/test.fun";
    FileStream stream = new FileStream(path, FileMode.Create);
    PlayerData data = new PlayerData(player);
    formatter.Serialize(stream, data);
    stream.Close();
}

public static PlayerData LoadPlayer() {
    string path = Application.persistentDataPath + "/test.fun";

    if (File.Exists(path)){
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream stream = new FileStream(path, FileMode.Open);

        PlayerData data = formatter.Deserialize(stream) as PlayerData;
        stream.Close();

        return data;

    } else
    {
        Debug.LogError("Save file not found in " + path);
        return null;
    }
}
}

给予的任何帮助都非常感谢

2 个答案:

答案 0 :(得分:0)

我编写了一些工作代码供您尝试,我尝试使其尽可能地易于编辑,以便使它适合您的用例。

Edit:您需要在Assets中创建文件夹SaveData,否则它将不起作用,但是它也会在调试日志中告诉您。

public void SavePlayerData()
{
    // Create the folder path.
    string t_Path = "Assets/SaveData";
    // Specify the file name.
    string t_FileName = "save.txt";

    if( AssetDatabase.IsValidFolder( t_Path ) )
    {
        Debug.Log( "Found: " + t_Path );

        // Create the file path.
        t_FileName = t_Path + "/" + t_FileName;

        if ( AssetDatabase.FindAssets( "save.txt" ).Length > 0)
        {
            // Create the actual text file.
            TextAsset t_NewFile = new TextAsset( "" );
            AssetDatabase.CreateAsset( t_NewFile, t_FileName );
        }          

        // Add text to the created file:
        string t_CopyRight = "" +
        "//////////////////////////////////////  \n" +
        "//    This save file is created     //  \n" +
        "//                by                //  \n" +              // Copyright text.
        "//               Livo               //  \n" +
        "//////////////////////////////////////  \n" +
        " \n";

        string t_Text = "This is the frist text value";             // Random PlayerSaveData text.
        string t_DiffrentText = "This is the second text value";    // Random Level text.
        string t_Location = "x=100,y=0,z=250";                      // Random Transform.position text.
        // You can add your playerdata to these strings.

        t_FileName = t_FileName.Replace( "Assets", "" );
        File.WriteAllText( Application.dataPath + t_FileName,   t_CopyRight + "\n" +
                                                                "[PlayerSaveData]\n" + t_Text + "\n\n" +
                                                                "[Level]\n" + t_DiffrentText + "\n\n" +
                                                                "[Location]\n" + t_Location );
        AssetDatabase.SaveAssets( );
        AssetDatabase.Refresh( );
    }
    else
    {
        Debug.Log( "There is no folder with that name: " + t_Path );
    }
}

这是导入保存数据的方法:

public void LoadPlayerData()
{
    string t_Path = "Assets/SaveData/save.txt";

    //Read the text from directly from the test.txt file
    StreamReader t_Reader = new StreamReader( t_Path );
    Debug.Log( t_Reader.ReadToEnd( ) );
    t_Reader.Close( );
}

顺便说一句:代码每次运行都会覆盖保存数据,但是我想这就是您想要的保存文件。如果您要使用其他保存文件,只需为t_FileName

动态命名

答案 1 :(得分:0)

您没有显示播放器数据的代码。我想你也像我一样犯了一个错误。您忘记编写[可序列化]并包括使用System。可以像我一样为您工作