序列化异常和 IOException:路径上的共享冲突



我在尝试为游戏创建保存和加载系统时遇到以下两个错误。

"序列化异常:程序集中的类型 UnityEngine.MonoBehavior UnityEngine.CoreModule,版本=0.0.0.0,区域性=中性,公钥令牌=null 未标记为可序列化。 System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, StreamingContext context) (at/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization/FormatterServices.cs:101) System.Runtime.Serialization.Formatters.Binary.CodeGenerator.GenerateMetadataTypeInternal (System.Type type, StreamingContext context) (at/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 type, StreamingContext context) (at

IOException:在路径上共享冲突 C:\Users\Sam\AppData\LocalLow\DefaultCompany\Game\test.txt System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at/Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320) System.IO.FileStream..ctor (系统字符串路径,文件模式模式) (包装器远程处理调用与检查)System.IO.FileStream:.ctor (string,System.IO.FileMode) SaveSystem.SavePlayer (.玩家玩家)(在资产/脚本/保存系统.cs:22) Player.SavePlayer () (at Assets/Scripts/Player.cs:47) Player.Update () (at Assets/Scripts/Player.cs:13)

这是保存和加载方法 保存以下系统类

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;
}
}
}

非常感谢给予的任何帮助

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

Edit:您需要在资产中创建文件夹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 + "nn" +
"[Level]n" + t_DiffrentText + "nn" +
"[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指定一个新名称

您没有显示玩家数据的代码。我想你也犯了我的错误。您忘记编写 [可序列化] 并包括使用系统。愿这对我有用的你

最新更新