Unity using C # serialization and deserialization to store game data

Using the sequence of stored game data, easy to move objects from one place to another (transfer file and then deserialized read) so that the game data is not easily tampered directly.

First we create a script named GameData of:

using UnityEngine;
using System.Collections;

[System.Serializable]//序列化
public class GameData {
	private bool isFristGame;//是否第一次运行游戏
	private bool isMusicOn;//是否打开音乐
	private int[] bestScoreArr;//分数记录

	public void SetIsFristGame(bool isFirstGame){
		this.isFristGame = isFristGame;
	}
	public void SetIsMusicOn(bool isMusicOn){
		this.isMusicOn = isMusicOn;
	}
	public void SetBestScoreArr(int[] bestScoreArr){
		this.bestScoreArr = bestScoreArr;
	}

	public bool GetIsFristGame( ){
		return isFristGame;
	}
	public bool GetIsMusicOn( ){
		return isMusicOn;
	}
	public int[] GetBestScoreArr( ){
		return bestScoreArr;
	}
}

Secondly, we create a script named GameManager to read and modify data:

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

public class GameManager : MonoBehaviour {
	public static GameManager Instance;
	public bool IsGameStarted{ get; set;}//记录游戏是否开始
	public bool IsGameOver{ get; set;}//记录游戏是否结束
	public bool IsPause{ get; set;}//记录游戏是否暂停

	private GameData data;

	private bool isFristGame;
	private bool isMusicOn;
	private int[] bestScoreArr;

	private void Awake(){
		Instance = this;
		InitGameData ();//初始化游戏数据
}

	/// <summary>
	/// 初始化游戏数据
	/// </summary>
	private void InitGameData(){
		Read ();
		if (data != null) {
			isFristGame = data.GetIsFristGame ();
		} else {
			isFristGame = true;
		}
		//如果第一次开始游戏
		if (isFristGame) {
			isFristGame = false;
			isMusicOn = true;
			bestScoreArr = new int[3];
			data = new GameData ();//否则Save方法里会报空指针
			Save ();
		} else {
			isMusicOn = data.GetIsMusicOn ();
			bestScoreArr = data.GetBestScoreArr ();
		}
	}
	
	/// <summary>
	/// 储存数据
	/// </summary>
	private void Save(){
		try {
			BinaryFormatter bf=new BinaryFormatter();
			using(FileStream fs=File.Create(Application.persistentDataPath+"/GameData.data")){//使用using可以自动释放流
				data.SetBestScoreArr(bestScoreArr);
				data.SetIsFristGame(isFristGame);
				data.SetIsMusicOn(isMusicOn);
				bf.Serialize(fs,data);//将类的数据序列化写进本地
			}
		} catch (System.Exception ex) {
			Debug.Log (ex.Message);
		}
	}
	
	/// <summary>
	/// 读取数据
	/// </summary>
	private void Read(){
		try {
			BinaryFormatter bf=new BinaryFormatter();
			using(FileStream fs=File.Open(Application.persistentDataPath+"/GameData.data",FileMode.Open)){
				data=(GameData)bf.Deserialize(fs);//反序列化
			}
		} catch (System.Exception ex) {
			Debug.Log (ex.Message);
		}
	}
	
	/// <summary>
	/// 重置数据
	/// </summary>
	public void ResetData(){
		isFristGame = false;
		isMusicOn = true;
		bestScoreArr = new int[3];
		Save ();
	}
}

NOTE: Modify call the Save () method After the data can not be directly serialized unity specific data type (e.g. Vector3, Quaternion), must be converted type. Otherwise it will error: "SerializationException: Type UnityEngine.XXX is not marked as Serializable."

Released three original articles · won praise 6 · views 1321

Guess you like

Origin blog.csdn.net/weixin_36330051/article/details/104223495