質問編集履歴
1
スクリプトの記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -95,3 +95,143 @@
|
|
95
95
|
}
|
96
96
|
|
97
97
|
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
### 一応できた方法
|
102
|
+
|
103
|
+
うんこみたいな方法ではありますが一応できました(?)ただ保存してある配列を取得して、それをそのまま新しいSaveインスタンスにぶち込んで、最後の要素にセーブしたい値をぶちこむという力技なのでもっとスタイリッシュな方法分かる方がいれば回答お願いします。
|
104
|
+
|
105
|
+
```C#
|
106
|
+
|
107
|
+
[System.Serializable]
|
108
|
+
|
109
|
+
public class SaveData
|
110
|
+
|
111
|
+
{
|
112
|
+
|
113
|
+
public int number;
|
114
|
+
|
115
|
+
public string name;
|
116
|
+
|
117
|
+
public StageInfo[] stageInfos;
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
[System.Serializable]
|
122
|
+
|
123
|
+
public class StageInfo
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
public string id_stage;
|
128
|
+
|
129
|
+
public bool clear;
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
public class SaveLoad : MonoBehaviour
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
string filePath;
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
void Awake()
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
filePath = Application.persistentDataPath + "/savedata.json";
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
public void Save()
|
154
|
+
|
155
|
+
{
|
156
|
+
|
157
|
+
SaveData save = new SaveData();
|
158
|
+
|
159
|
+
save.number = 0;
|
160
|
+
|
161
|
+
save.name = "AAAA";
|
162
|
+
|
163
|
+
Save2(save); //配列のセーブ
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
string json = JsonUtility.ToJson(save);
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
StreamWriter streamWriter = new StreamWriter(filePath);
|
172
|
+
|
173
|
+
streamWriter.Write(json);
|
174
|
+
|
175
|
+
streamWriter.Flush();
|
176
|
+
|
177
|
+
streamWriter.Close();
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
public void Save2(SaveData save)
|
184
|
+
|
185
|
+
{
|
186
|
+
|
187
|
+
StreamReader reader = new StreamReader(filePath);
|
188
|
+
|
189
|
+
string json = reader.ReadToEnd(); //セーブしてあるJsonを取得
|
190
|
+
|
191
|
+
reader.Close();
|
192
|
+
|
193
|
+
SaveData load = new SaveData();
|
194
|
+
|
195
|
+
load = JsonUtility.FromJson<SaveData>(json);
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
int length = load.stageInfos.Length; //セーブしてある配列の要素数取得
|
200
|
+
|
201
|
+
save.stageInfos = new StageInfo[length + 1]; //要素数+1っで生成
|
202
|
+
|
203
|
+
for (int i = 0;i < length + 1;i++)
|
204
|
+
|
205
|
+
{
|
206
|
+
|
207
|
+
if(i == length) //配列の最後の要素の場合
|
208
|
+
|
209
|
+
{
|
210
|
+
|
211
|
+
save.stageInfos[i] = new StageInfo();
|
212
|
+
|
213
|
+
save.stageInfos[i].id_stage = i.ToString();
|
214
|
+
|
215
|
+
save.stageInfos[i].clear = true;
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
else //ロードした要素をそのまま入れ直す
|
220
|
+
|
221
|
+
{
|
222
|
+
|
223
|
+
save.stageInfos[i] = new StageInfo();
|
224
|
+
|
225
|
+
save.stageInfos[i].id_stage = load.stageInfos[i].id_stage;
|
226
|
+
|
227
|
+
save.stageInfos[i].clear = load.stageInfos[i].clear;
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
```
|