2016-03-19 2 views
0

Je veux enregistrer les données de mon jeu dans le fichier après le début du jeu, puis après le jeu de fermeture/démarrage, je veux charger les données de ce fichier. J'ai essayé d'utiliser la solution this, mais quand je commence et ensuite arrêter la simulation, je ne vois pas le fichier indicatorsInfo.dat, mais Debug.Log() dit, qu'il existe. De toute façon il ne charge pas les données, qu'est ce qui ne va pas?Impossible de sauvegarder les données dans un fichier, puis de le charger dans Unity3d

enter image description here

using UnityEngine; 
    using System; 
    using System.Runtime.Serialization.Formatters.Binary; 
    using System.IO; 

    public class GAMEMANAGERFileIO : MonoBehaviour 
    { 
     void OnEnable() 
     {   
      LoadFromFile(); 
      SaveToFile(); 
     } 

     void OnDisable() 
     {   
      SaveToFile(); 
     } 

     public void SaveToFile() 
     { 
      GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER"); 
      GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();    

      BinaryFormatter binaryFormatter = new BinaryFormatter(); 
      FileStream fileStream = File.Create(Application.persistentDataPath + "/indicatorsInfo.dat"); // open file 
      IndicatorsInfo indicatorsInfo = new IndicatorsInfo(); 

      // Initialise data of the class IndicatorsInfo 
      //... 

      // save data to the file. Serialize([to where we want to save], [what we want to save]) 
      binaryFormatter.Serialize(fileStream, indicatorsInfo); 

========== EDIT 1 ====================================================== 
     File.WriteAllText("C:/Users/dima/Desktop/exampleSaveData.txt", 
      indicatorsInfo.walkSpeedTemfFile.ToString() + ", " + 
      indicatorsInfo.runSpeedTemfFile + ", " + 
      indicatorsInfo.jumpForceTemfFile + ", " + 
      indicatorsInfo.enduranceTemfFile); 
======================================================================== 

      fileStream.Close(); 
      Debug.Log("saved"); // this works 
     } 

     public void LoadFromFile() 
     { 
      // check if file exisits before we will try to open it 
      if(File.Exists(Application.persistentDataPath + "/indicatorsInfo.dat")) 
      { 
       GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER"); 
       GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>(); 

       BinaryFormatter binaryFormatter = new BinaryFormatter(); 
       FileStream fileStream = File.Open(Application.persistentDataPath + "/indicatorsInfo.dat", FileMode.Open); // open file 
       IndicatorsInfo indicatorsInfo = (IndicatorsInfo)binaryFormatter.Deserialize(fileStream); // read from file 
       fileStream.Close(); // close file 

       // Initialise local data with values from the class IndicatorsInfo 
       //... 

========== EDIT 1 ====================================================== 
      File.ReadAllText("C:/Users/dima/Desktop/exampleSaveData.txt"); 
======================================================================== 
      } 
     } 
    } 

    // clear class with data, which we will store to file 
    [Serializable] 
    class IndicatorsInfo 
    { 
     //... 
    } 

EDIT 1

J'ai ajouté File.WriteAllText() et File.ReadAllText(). Mais j'ai 2 problèmes:

  1. Je dois créer moi-même le fichier txt avant de pouvoir l'enregistrer;
  2. Je peux enregistrer des données dans un fichier (valeurs de 4 variables), mais je ne peux pas le charger.

enter image description here

Répondre

3

Il est incroyablement facile à lire et écrire des fichiers dans l'unité.

// IO crib sheet.. 
// filePath = Application.persistentDataPath+"/"+fileName; 
// check if file exists System.IO.File.Exists(f) 
// write to file File.WriteAllText(f,t) 
// delete the file if needed File.Delete(f) 
// read from a file File.ReadAllText(f) 

c'est tout ce qu'il y a à faire.

string currentText = File.ReadAllText(filePath); 

NOTE BIEN ...........

// filePath = Application.persistentDataPath+"/"+fileName; 
// YOU MUST USE "Application.persistentDataPath" 
// YOU CANNOT USE ANYTHING ELSE 
// NOTHING OTHER THAN "Application.persistentDataPath" WORKS 
// ALL OTHER OPTIONS FAIL ON ALL PLATFORMS 
// YOU CAN >ONLY< USE Application.persistentDataPath IN UNITY 
+0

Mais j'utilise 'Application.persistentDataPath',' binaryFormatter.Serialize (,) 'écrire et' binaryFormatter.Deserialize() 'lire. Je n'utilise pas 'File.WriteAllText (f, t)', 'File.ReadAllText (f)'. Est-ce une raison pour laquelle cela ne fonctionne pas? – dima

+1

binaryFormatter.Serialize/Deserialize ne sauvegarde pas les données, elles les sérialisent, ce qui les transforme en une forme binaire. – Everts