回答編集履歴

1

tilesがシリアライズできないことへの対策を追加

2020/04/26 05:35

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -240,7 +240,19 @@
240
240
 
241
241
 
242
242
 
243
- そして`TileMapManager2`の変更点として、まず
243
+ そして`TileMapManager2`の変更点として、まず宣言部分を
244
+
245
+
246
+
247
+ ```C#
248
+
249
+ public class TileMapManager2 : MonoBehaviour, ISerializationCallbackReceiver {
250
+
251
+ ```
252
+
253
+
254
+
255
+ とし、
244
256
 
245
257
 
246
258
 
@@ -248,7 +260,39 @@
248
260
 
249
261
  // ゲーム実行時にタイルを得るため、タイルへの参照をシリアライズして持っておく
250
262
 
263
+ [SerializeField, HideInInspector] private Sprite[] tileKeys;
264
+
265
+ [SerializeField, HideInInspector] private Tile[] tileValues;
266
+
251
- [SerializeField, HideInInspector] private Dictionary<Sprite, Tile> tiles = new Dictionary<Sprite, Tile>();
267
+ private Dictionary<Sprite, Tile> tiles = new Dictionary<Sprite, Tile>();
268
+
269
+
270
+
271
+ public void OnBeforeSerialize()
272
+
273
+ {
274
+
275
+ tileKeys = tiles.Keys.ToArray();
276
+
277
+ tileValues = tiles.Values.ToArray();
278
+
279
+ }
280
+
281
+
282
+
283
+ public void OnAfterDeserialize()
284
+
285
+ {
286
+
287
+ foreach ((Sprite key, Tile value) in tileKeys.Zip(tileValues, (key, value) => (key, value)))
288
+
289
+ {
290
+
291
+ tiles.Add(key, value);
292
+
293
+ }
294
+
295
+ }
252
296
 
253
297
 
254
298