美文网首页
Odin Inspector 系列教程 --- 自定义Odin序

Odin Inspector 系列教程 --- 自定义Odin序

作者: su9257_海澜 | 来源:发表于2019-12-06 21:21 被阅读0次

    前言:在上一章节中笔者讲解了Odin Inspector 系列教程 --- 初识Odin序列化,Odin是通过继承SerializedMonoBehaviour等类来达到序列化的效果,这次笔者介绍不通过继承SerializedMonoBehaviour来实现序列化的方式

    下面的示例以MonoBehaviour和ScriptableObject为例子,使用比较简单,只是在需要的实现序列化的脚本上继承ISerializationCallbackReceiverCustomSerializedScriptableObject,并实现相关接口

    固定写法,示例如下

    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);
        }
    }
    

    更多教程内容详见:革命性Unity 编辑器扩展工具 --- Odin Inspector 系列教程

    相关文章

      网友评论

          本文标题:Odin Inspector 系列教程 --- 自定义Odin序

          本文链接:https://www.haomeiwen.com/subject/qaiegctx.html