-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialization.cs
More file actions
65 lines (57 loc) · 1.57 KB
/
Copy pathSerialization.cs
File metadata and controls
65 lines (57 loc) · 1.57 KB
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
/*
*┌──────────────────────────────────┐
*│ Copyright(C) 2016 by Antiphon.All rights reserved. │
*│ Author:by Locke Xie 2017-01-06 │
*└──────────────────────────────────┘
*
* 功 能: Json系列化格式转换
* 类 名: Serialization.cs
*
* 修改历史:
*
*
*/
using UnityEngine;
using System.Collections.Generic;
using System;
// List<T>
[Serializable]
public class Serialization<T>
{
[SerializeField]
List<T> target;
public List<T> ToList() { return target; }
public Serialization(List<T> target)
{
this.target = target;
}
}
// Dictionary<TKey, TValue>
[Serializable]
public class Serialization<TKey, TValue>: ISerializationCallbackReceiver
{
[SerializeField]
List<TKey> keys;
[SerializeField]
List<TValue> values;
Dictionary<TKey, TValue> target;
public Dictionary<TKey, TValue> ToDictionary() { return target; }
public Serialization(Dictionary<TKey, TValue> target)
{
this.target = target;
}
public void OnBeforeSerialize()
{
keys = new List<TKey>(target.Keys);
values = new List<TValue>(target.Values);
}
public void OnAfterDeserialize()
{
var count = Math.Min(keys.Count, values.Count);
target = new Dictionary<TKey, TValue>(count);
for(var i = 0; i < count; ++i)
{
target.Add(keys[i], values[i]);
}
}
}