Я сделал несколько рабочих кодов для вас, я постарался сделать так, чтобы их было так легко редактировать, чтобы вы могли сделать так, чтобы они работали для вашего случая использования.
Edit:
Вам нужно создатьпапка SaveData в Assets, иначе она не будет работать, но она также сообщит вам об этом в журнале отладки.
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