コード例 #1
0
        /// <summary>
        /// Loads the data and puts it into the display to show it has loaded the entered values
        /// </summary>
        public void LoadGame()
        {
            ExampleSaveData loadData = ExampleSaveManager.LoadGame();

            displayPlayerName.text     = loadData.examplePlayerName;
            displayPlayerHealth.text   = loadData.examplePlayerHealth.ToString();
            displayPlayerPosition.text = loadData.examplePlayerPosition.Vector3.ToString();
            displayPlayerShield.text   = loadData.exampleCustomClass.shieldAmount.ToString();
        }
コード例 #2
0
        /// <summary>
        /// EXAMPLE ONLY | Static | Saves the game, using the ExampleSaveData class.
        /// </summary>
        /// <param name="_data">The ExampleSaveData to save.</param>
        public static void SaveGame(ExampleSaveData _data)
        {
            string SavePath = Application.persistentDataPath + "/savefile.example";

            // Erased the old save file, done to avoid problems loading as the class changing will cause an error is not done.
            if (File.Exists(SavePath))
            {
                File.Delete(SavePath);
            }

            BinaryFormatter Formatter = new BinaryFormatter();
            FileStream      Stream    = new FileStream(SavePath, FileMode.OpenOrCreate);

            Formatter.Serialize(Stream, _data);
            Stream.Close();
        }
コード例 #3
0
        /// <summary>
        /// EXAMPLE ONLY | Static | Loads the game and returns an instance of the ExampleSaveData class
        /// </summary>
        /// <returns>An instance of the SaveData class with the loaded values</returns>
        public static ExampleSaveData LoadGame()
        {
            string SavePath = Application.persistentDataPath + "/savefile.example";

            if (File.Exists(SavePath))
            {
                BinaryFormatter Formatter = new BinaryFormatter();
                FileStream      Stream    = new FileStream(SavePath, FileMode.Open);

                ExampleSaveData _data = Formatter.Deserialize(Stream) as ExampleSaveData;

                Stream.Close();

                return(_data);
            }
            else
            {
                Debug.LogError("Save file not found!");
                return(null);
            }
        }
コード例 #4
0
        /// <summary>
        /// Saves the data by making a new save data and passing the values entered into the scene into it, you don't have to edit all the values each time you save, this example does.
        /// NOTE: int/float.phase() is used here as we are using input fields in the scene, you'd normally have the correct value type to hand, with UI Input fields we don't, so we have to convert them in this example.
        /// </summary>
        public void SaveGame()
        {
            // For you this would be ' SaveData ' not ' ExampleSaveData '
            ExampleSaveData saveData = new ExampleSaveData();

            // Passing in values to be saved
            saveData.examplePlayerName   = playerName.text;
            saveData.examplePlayerHealth = float.Parse(playerHealth.text);

            // Creating a SaveVector3 and setting the Vector3 up before saving it.
            SaveVector3 exampleVec = new SaveVector3();

            exampleVec.Vector3             = new Vector3(float.Parse(playerPosition0.text), float.Parse(playerPosition1.text), float.Parse(playerPosition2.text));
            saveData.examplePlayerPosition = exampleVec;

            exampleClass.shieldAmount   = int.Parse(playerShield.text);
            saveData.exampleCustomClass = exampleClass;

            // you would just call ' SaveManager.SaveGame(saveData) ' for the sake of not requiring the project to be set up one way to show the asset working...
            // ...there is an example save manager on this script which runs just like the normal one.
            ExampleSaveManager.SaveGame(saveData);
        }