系统提供的序列化方法
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class CubeScript : MonoBehaviour
{
#region
[Serializable]
public class PersonInfo
{
public string name;
public int age;
public string sex;
}
// 无法在面板上显示(序列化不支持字典类型的属性,可利用序列化监听接口进行监听)
[SerializeField]
private Dictionary<string, object> personInfo01 = new Dictionary<string, object>
{
{ "name", "李四" },
{ "age", 12 },
{ "sex", "男" },
};
// 可在面板上显示(实际项目中用到的数据可能比较复杂可以将对象放到类中进行管理)
// 参与序列化可向类上添加[System.Serializable]特性,不参与序列化可在属性上添加[System.NonSerialized]特性
public PersonInfo personInfo02 = new PersonInfo
{
name = "张三",
age = 10,
sex = "女"
};
// 无法在面板上显示(已配置不参与序列化)
[NonSerialized]
public PersonInfo personInfo03 = new PersonInfo
{
name = "王五",
age = 7,
sex = "女"
};
#endregion
private void OnValidate()
{
Debug.Log("OnValidate ---> 编辑面板信息发生变化");
Debug.Log($"{personInfo01["name"]} ---> {personInfo01["age"]} {personInfo01["sex"]}");
Debug.Log($"{personInfo02.name} ---> {personInfo02.age} {personInfo02.sex}");
Debug.Log($"{personInfo03.name} ---> {personInfo03.age} {personInfo03.sex}");
}
}
序列化/反序列化监听
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class CubeScript : MonoBehaviour, ISerializationCallbackReceiver
{
#region
[SerializeField] private List<string> keys = new List<string>();
[SerializeField] private List<Sprite> values = new List<Sprite>();
public Dictionary<string, Sprite> spriteObj = new Dictionary<string, Sprite>
{
{ "sprite1", null },
{ "sprite2", null },
{ "sprite3", null },
};
public void OnBeforeSerialize()
{
keys.Clear();
values.Clear();
keys = spriteObj.Select(pair => pair.Key).ToList();
values = spriteObj.Select(pair => pair.Value).ToList();
}
public void OnAfterDeserialize()
{
spriteObj.Clear();
int count = keys.Count < values.Count ? keys.Count : values.Count;
for (int i = 0; i < count; i++)
{
spriteObj[keys[i]] = values[i];
}
}
#endregion
private void OnValidate()
{
Debug.Log("OnValidate ---> 编辑面板信息发生变化");
Debug.Log(string.Join("---", spriteObj.Select(pair => $"{pair.Key}:{pair.Value}").ToList()));
}
}
#if
[CustomEditor(typeof(CubeScript))]
class CubeScriptEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
// 更新最新数据
serializedObject.Update();
// 获取数据信息
SerializedProperty propertyKeys = serializedObject.FindProperty("keys");
SerializedProperty propertyValues = serializedObject.FindProperty("values");
int size = propertyKeys.arraySize;
for (int i = 0; i < size; i++)
{
var key = propertyKeys.GetArrayElementAtIndex(i);
var value = propertyValues.GetArrayElementAtIndex(i);
GUILayout.BeginHorizontal();
key.stringValue = EditorGUILayout.TextField("key", key.stringValue);
value.objectReferenceValue = EditorGUILayout.ObjectField("value", value.objectReferenceValue, typeof(Sprite), false);
GUILayout.EndHorizontal();
}
if (GUILayout.Button("+"))
{
(target as CubeScript)?.spriteObj.Add(size.ToString(), null);
}
serializedObject.ApplyModifiedProperties();
}
}
#endif