Unity 3D archives game data through serialization

Posted by pandaweb on Sat, 02 Nov 2019 22:34:45 +0100

In the process of using u3d to develop some stand-alone games, we will involve the storage and loading of game data. In general, if the stored data is not complex, we can use PlayerPrefs, but sometimes the data involved is more complex, and it is difficult to deal with using PlayerPrefs, so we can choose to use serialization to archive the game data. Next, I implement loading data and archiving data through small cases.

First, let's create a script, which is the carrier of game data and can be used for serialization. The code is as follows:

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

[System.Serializable]
public class Gamedata
{
    // Three game data
    public int data1;
    public string  data2;
    public bool data3;

    // Singleton mode
    private Gamedata()
    {
        data1=0;      //  Score
        data2 = "Lin Luo Shang";    // Nickname?
        data3 = true;     // Whether to obtain [Egyptian gem]
    }
    // object
    private static Gamedata gamedata;
    //Interface 1
    public static  Gamedata GetGamedataInstance()
    {
        if (gamedata == null )
        {
            gamedata=new  Gamedata();
        }
        return gamedata;
    }
    //Interface 2
    public static void  SetGamedataInstance (BinaryFormatter bf,FileStream fs)
    {
        gamedata = (Gamedata) bf.Deserialize(fs);
    }
}

The next one is to save, load game data and display game data on the panel.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.UI;

public class Gamedatasava_load : MonoBehaviour
{
    public Text data1;
    public Text data2;
    public Text data3;
    private string datapath;
    void Start()
    {
        DontDestroyOnLoad(this);
        Setpath();    // Set path according to platform
        Loaddata(); //Loading data
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Gamedata.GetGamedataInstance().data1++;
        }

        if (Input.GetMouseButtonDown(1))
        {
            Gamedata.GetGamedataInstance().data3 = !Gamedata.GetGamedataInstance().data3;
        }

        if (Input.GetMouseButtonDown(2))
        {
            Gamedata.GetGamedataInstance().data2 += "Luo";
        }
    }
    /// <summary>
    /// Load data to panel
    /// </summary>
    public void OnGUI()
    {
        data1.text = ""+Gamedata.GetGamedataInstance().data1;
        data2.text = ""+Gamedata.GetGamedataInstance().data2;
        data3.text = ""+Gamedata.GetGamedataInstance().data3;
    }
    /// <summary>
    /// Save game data
    /// </summary>
    public void Savadata()
    {
       BinaryFormatter bf=new BinaryFormatter();
       if (File.Exists(datapath))
       {
           File.Delete(datapath);
       }
       FileStream fs=new FileStream(datapath,FileMode.Create);
       bf.Serialize(fs,Gamedata.GetGamedataInstance());
       fs.Close();
    }
    /// <summary>
    /// Load game data
    /// </summary>
    public void Loaddata()
    {
        // Determine whether there is a file on the path
        if (File.Exists(datapath))
        {
            BinaryFormatter bf=new BinaryFormatter();
            FileStream fs=new FileStream(datapath ,FileMode.Open);
            Gamedata.SetGamedataInstance(bf,fs);
            fs.Close();
        }
    }
    void Setpath()
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            datapath = Application.persistentDataPath + "/Gamedata.gb";
        }

        if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            datapath = Application.streamingAssetsPath + "/Gamedata.gb";
        }
    }
    private void OnDestroy()
    {
        Savadata();
    }
}

The last one is used to mount on the button and exit the game, which is quite simple.

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

public class Quit : MonoBehaviour
{
    public   void Click ()
    {
        Application.Quit();
    }
}

Next let's look at the file structure (because it involves loading paths)

Operation effect:

The display effect of mobile phone is the same, which can realize the storage and loading of game data.

For this article, the writing is very simple, not detailed enough, a lot of forgiveness.

Topics: Java Android Mobile