using UnityEngine;
using System.Collections.Generic;
public class test : MonoBehaviour, ISerializationCallbackReceiver
{
public List<int> _keys;
public List<string> _values;
//Unity doesn't know how to serialize a Dictionary
public Dictionary<int, string> _myDictionary = new Dictionary<int, string>
{
{3, "I"},
{4, "Love"},
{5, "Unity"}
};
// 一改_keys,_values就调用
public void OnAfterDeserialize()
{
Debug.Log(" ** OnAfterDeserialize **");
_myDictionary.Clear();
for (int i = 0; i != System.Math.Min(_keys.Count, _values.Count); i++)
_myDictionary.Add(_keys[i], _values[i]);
}
// 每帧调用
public void OnBeforeSerialize()
{
//Debug.Log(" ** OnBeforeSerialize **");
//_keys.Clear();
//_values.Clear();
//foreach (var kvp in _myDictionary)
//{
// _keys.Add(kvp.Key);
// _values.Add(kvp.Value);
//}
}
void OnGUI()
{
foreach (var kvp in _myDictionary)
GUILayout.Label("Key: " + kvp.Key + " value: " + kvp.Value);
}
}
OnBeforeSerialize And OnAfterDeserialize
最新推荐文章于 2024-07-01 17:08:11 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)