Unity存档样例适用于PC、安卓、IOS

实现安卓、ios、Windows端存档功能,目前只在这三个平台测试可用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using UnityEngine;

public struct Storage
{
public DataSlot[] dataSlot;//存档槽
public GlobalData globalData;//全局数据
};
public struct DataSlot
{
public int fraction;//分数
public int blood;//血量
public int attackPower;//攻击力
public int defenseForce;//防御力
};
public struct GlobalData
{
public int volume;//音量
public int version;//版本号
public int firstPlay;//第一次运行
public int nowSlot;//当前存档下标
};

public class StorageDemo : MonoBehaviour {

public static Storage storage;

// Use this for initialization
void Start () {
InitStorageData();
Load();
}

// Update is called once per frame
void Update () {

}

public void InitStorageData()
{
storage.dataSlot = new DataSlot[3];
storage.globalData = new GlobalData();
InitDataSlot();
InitGlobalData();
}
public void InitDataSlot()
{
for (int i = 0; i < storage.dataSlot.Length; i++)
{
DataSlot dataSlot = new DataSlot();
dataSlot.fraction = 0;
dataSlot.blood = 0;
dataSlot.attackPower = 0;
dataSlot.defenseForce = 0;
storage.dataSlot[i] = dataSlot;
}
}

public void InitGlobalData()
{
GlobalData global = new GlobalData();
global.firstPlay = -1;
global.nowSlot = -1;
global.version = 0;
global.volume = 0;
storage.globalData = global;
}

public void Save()
{
var serializer = new XmlSerializer(typeof(Storage));
var stream = new FileStream(Application.persistentDataPath + "/StorageDemo", FileMode.Create);



using (stream)
{
serializer.Serialize(stream, storage);
}
}

public void Load()
{
var serializer = new XmlSerializer(typeof(Storage));
var stream = new FileStream(Application.persistentDataPath + "/StorageDemo", FileMode.Open);


using (stream)
{
storage = (Storage)serializer.Deserialize(stream);
}
}

public void DebugStorage()
{
for (int i = 0; i < storage.dataSlot.Length; i++)
{
Debug.Log("血槽" + i + "数值为:");
DataSlot dataSlot = storage.dataSlot[i];
Debug.Log("分数:" + dataSlot.fraction);
Debug.Log("血量:" + dataSlot.blood);
Debug.Log("攻击力:" + dataSlot.attackPower);
Debug.Log("防御力:" + dataSlot.defenseForce);
}
GlobalData globalData = storage.globalData;
Debug.Log("第一次运行游戏:"+ globalData.firstPlay);
Debug.Log("音量:" + globalData.volume);
Debug.Log("版本号:" + globalData.version);
Debug.Log("当前存档:" + globalData.nowSlot);
}
public void PlayGame()
{

for (int i = 0; i < storage.dataSlot.Length; i++)
{
DataSlot dataSlot = new DataSlot();
dataSlot.fraction = i + 1;
dataSlot.blood = i + 1;
dataSlot.attackPower = i + 1;
dataSlot.defenseForce = i + 1;
storage.dataSlot[i] = dataSlot;
}
GlobalData global = new GlobalData();
global.firstPlay = 1;
global.nowSlot = 1;
global.version = 0;
global.volume = 1;
storage.globalData = global;

}
public void OnGUI()
{
if (GUI.Button(new Rect(10, 0, 100, 50), "Save"))
{
Save();
}
if (GUI.Button(new Rect(10, 100, 100, 50), "Load"))
{
Load();
}
if (GUI.Button(new Rect(10, 200, 100, 50), "PlayGame"))
{
PlayGame();
}
if (GUI.Button(new Rect(10, 300, 100, 50), "DebugStorage"))
{
DebugStorage();
}
}

}

用法:

创建脚本StorageDemo.cs将上面代码粘贴到脚本中,脚本挂在游戏物体上,运行项目即可。

运行效果