Odin Inspector 系列教程 --- 自定义Odin序
2019-12-06 本文已影响0人
su9257_海澜
前言:在上一章节中笔者讲解了Odin Inspector 系列教程 --- 初识Odin序列化,Odin是通过继承
SerializedMonoBehaviour
等类来达到序列化的效果,这次笔者介绍不通过继承SerializedMonoBehaviour
来实现序列化的方式
下面的示例以MonoBehaviour和ScriptableObject为例子,使用比较简单,只是在需要的实现序列化的脚本上继承
ISerializationCallbackReceiver
和CustomSerializedScriptableObject
,并实现相关接口
固定写法,示例如下
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using System.Collections.Generic;
using UnityEngine;
[ShowOdinSerializedPropertiesInInspector]
public class CustomSerializedMonoBehaviour : MonoBehaviour, ISerializationCallbackReceiver, ISupportsPrefabSerialization
{
[SerializeField, HideInInspector]
private SerializationData serializationData;
SerializationData ISupportsPrefabSerialization.SerializationData { get { return this.serializationData; } set { this.serializationData = value; } }
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
UnitySerializationUtility.DeserializeUnityObject(this, ref this.serializationData);
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
UnitySerializationUtility.SerializeUnityObject(this, ref this.serializationData);
}
public Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
}
[ShowOdinSerializedPropertiesInInspector]
public class CustomSerializedScriptableObject : ScriptableObject, ISerializationCallbackReceiver
{
[SerializeField, HideInInspector]
private SerializationData serializationData;
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
UnitySerializationUtility.DeserializeUnityObject(this, ref this.serializationData);
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
UnitySerializationUtility.SerializeUnityObject(this, ref this.serializationData);
}
}