Donnerstag, 9. April 2020

Unity - concept of loading and saving (InputField) data

First experience about how to structure saving and loading of data - e.g. of an InputField

- create class of UserData with the properties to save and load
- create class of SaveSystem which covers LoadData() and SaveData() functions
- create empty GameObject and call it UserGameObject
- create class User and assign it per drag and drop to the UserGameObject
- add public InputField property to the User class to be linked with the InputField control.
- add the callback function of the On End Edit InpuField event to the User class.
- There you are going to put the new value of the InputField to the UserData object and call the SaveData() function to save the UserData object.

Snippets (different conversation)

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

public static class SaveSystem
{

    public static void SavePlayer(Player player)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/player.bin";
        try
        {
            if (File.Exists(path))
                File.Delete(path);
            FileStream fileStream = new FileStream(path, FileMode.Create);
            formatter.Serialize(fileStream, player.UserData);
            fileStream.Close();
            fileStream.Dispose();
            Debug.Log("Okay, saved settings");
        }
        catch (Exception ex)
        {
            Debug.LogError("Unable to save settings! " + ex.Message);
        }
    }

    public static PlayerData LoadPlayer()
    {
        PlayerData player = new PlayerData();
        string path = Application.persistentDataPath + "/player.bin";
        try
        {
            FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate);
            BinaryFormatter formatter = new BinaryFormatter();
            player = (PlayerData)formatter.Deserialize(fileStream);
            fileStream.Close();
            fileStream.Dispose();
            Debug.Log("Okay, Loaded settings.");
        }
        catch (Exception ex)
        {
            Debug.LogError("Unable to load settings! " + ex.Message);
        }
        return player;
    }


}


Data class

[System.Serializable]
public class PlayerData 
{
    public string Partnertoken;
    public string TempPartnerToken;

    public PlayerData()
    {
        this.Partnertoken = "";
        this.TempPartnerToken = "";
    }

}

GameObject class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Player : MonoBehaviour
{
    public PlayerData UserData;
    public InputField PartnertokenInputField;

    // Start is called before the first frame update
    void Start()
    {
        UserData = new PlayerData();
        UserData = SaveSystem.LoadPlayer();
        if (UserData == null)
            Debug.LogError("Code 202004100825 - UserData object is still null");
        else
            this.PartnertokenInputField.text = UserData.Partnertoken;
    }

    #region InputField Partnertoken

    public void OnPartnertokenChanged(string newToken)
    {
        UserData.TempPartnerToken = newToken;
    }

    public void OnPartnertokenEndEdit(string newToken)
    {
        UserData.TempPartnerToken = newToken;
        this.ShiftTempPartnertoken();
        SaveSystem.SavePlayer(this);
    }

    public void ShiftTempPartnertoken()
    {
        UserData.Partnertoken = UserData.TempPartnerToken;
    }

    #endregion
}


0 Kommentare:

Kommentar posten

Abonnieren Kommentare zum Post [Atom]

<< Startseite