Created
April 13, 2020 03:32
-
-
Save EdgarValfogo/a51ca15cd854652443d92ae912714405 to your computer and use it in GitHub Desktop.
Save system para Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.IO; // Trabalhar com arquivos | |
using System.Runtime.Serialization.Formatters.Binary; // Para trabalhar com serializacao para binarios | |
// Não podemos instanciar a classe | |
public static class SaveSystem | |
{ | |
private static string saveFilePath = Application.persistentDataPath + "/data.ps"; // Trabalha com a localização do executável, independente do SO | |
public static void SavePlayer( Player player ) { | |
BinaryFormatter formatter = new BinaryFormatter(); | |
string path = saveFilePath; | |
FileStream stream = new FileStream( path, FileMode.Create ); | |
PlayerData data = new PlayerData( player ); // Adicionar uma classe flagada com [System.Serializable] | |
formatter.Serialize( stream, data ); | |
stream.Close(); | |
} | |
public static PlayerData LoadPlayer() { | |
string path = saveFilePath; | |
if( File.Exists( path) ){ | |
BinaryFormatter formatter = new BinaryFormatter(); | |
FileStream stream = new FileStream( path, FileMode.Open ); | |
PlayerData data = formatter.Deserialize( stream ) as PlayerData; | |
stream.Close(); | |
Debug.Log( data.position[0] + ", " + data.position[1] + ", " + data.position[2] ); | |
return data; | |
} else { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment