一般游戏的存档,即数据的持久化,网游存储在服务器端的数据库,单机游戏的话存在本地文件中;
数据的存与取也即数据的序列化与反序列化,
方案一:是把数据直接利用C#语言自带的序列化库库函数 system.serialize 做二进制格式的存取 ;参考:https://blog.csdn.net/qq_42672770/article/details/115067914
方案二:Jason格式
方案三:protoBuff/ flatBuffer
这里是Jason的方案:
/* ***********************************************
* Discribe:
* Author:PeterGao
* CreateTime:2020-02-25 16:11:48
* Edition:1.0
* ************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 游戏数据基类
public class Data
{
public int level;
public int money;
public List bagitems = new List(); // 物品背包
}
/* ***********************************************
* Discribe:
* Author:PeterGao
* CreateTime:2020-02-26 09:34:51
* Edition:1.0
* ************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 定义道具类型枚举
public enum Type
{
Weapon,
Shoe
}
// 物品道具基类
public class Item
{
public int id;
public string name;
public Type type;
public Item()
{
}
public Item(string n, Type t)
{
this.type = t;
this.name = n;
}
}
/* ***********************************************
* Discribe:
* Author:PeterGao
* CreateTime:2020-02-25 08:36:50
* Edition:1.0
* ************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using LitJson;
using System.IO;
using UnityEngine.SceneManagement;
// 存档实现脚本
public class Test2 : MonoBehaviour
{
Data data = new Data();
public Text[] Name;
public Text itemtext;
public Button[] Up;
// UI显示
void Init()
{
Name[0].text = "等级: " + data.level.ToString(); // 显示等级
Name[1].text = "金币: " + data.money.ToString();
Up[0].onClick.AddListener(() => { data.level++; }); // 点击加1
Up[1].onClick.AddListener(() => { data.money++; });
string itemname = ""; // 显示道具名称
foreach (var item in data.bagitems)
{
itemname += item.name + " ";
}
itemtext.text = itemname;
}
// UI刷新
private void Update()
{
Name[0].text = "等级: " + data.level.ToString();
Name[1].text = "金币: " + data.money.ToString();
string itemname = "";
foreach (var item in data.bagitems)
{
itemname += item.name + " ";
}
itemtext.text = itemname;
}
// Start is called before the first frame update
void Start()
{
Init();
}
// 点击后向背包列表增加一个物品
public void GetItem()
{
data.bagitems.Add(new Item("长剑", Type.Weapon));
}
// 数据存档
public void SaveData()
{
// 数据转换成json字符串
string json = JsonMapper.ToJson(data);
if (!File.Exists("data.json"))
{
File.Create("data.json").Close();
Debug.Log("Create data.json");
}
// 将Json字符串以文件流的方式写入并覆盖原Json文件
using (StreamWriter sw= new StreamWriter(new FileStream("data.json", FileMode.Truncate)))
{
sw.Write(json);
sw.Close();
Debug.Log("Writen data to Json File successfully !");
}
}
// 加载存档
public void LoadData()
{
if (!File.Exists("data.json"))
{
Debug.Log("No File found !");
return;
}
// 将存档文件以文件流的方式读取并转换成json字符串,再转换成数据对象
using (StreamReader sr = new StreamReader(new FileStream("data.json", FileMode.Open)))
{
string json = sr.ReadLine();
data = JsonMapper.ToObject(json);
sr.Close();
Debug.Log("Load data successfully ! ");
}
}
// 切换场景
private void OnGUI()
{
//if (GUI.Button(new Rect(125, -125, 200, 60), "Turn to Scene1"))
if (GUILayout.Button("Turn to Scene1"))
{
SceneManager.LoadScene("Scene1");
Debug.Log("切换到场景1");
}
}
}