ScriptableObject 序列化使用案例

定义


    [Serializable]
    public class VirtualSunConfig : ScriptableObject
    {
        public VirtualSunMono.Mode m_Mode;
        public int m_AloneChar;
        public string[] m_CharList;
        public List m_Monos;
        public List m_AloneList;

        public Color m_MainColor;
        public Color m_RampColor;
        [ColorUsage(true, true)]
        public Color m_RampHDRColor;
        public Color m_FaceShadowColor;
        public Color m_OutlineColor;
        public bool m_AsyncFaceRamp;
        public Vector3 m_LightInfo;
        //public Vector3 m_LightInfoShadow;

        [Range(0, 1)]
        public float m_Metallic;
        [Range(0, 10)]
        public float m_HighLightStrength;
        [Range(0, 1)]
        public float m_MetallicAtten;
        [Range(0, 10)]
        public float m_HighLightLineStrenth;
        [Range(0, 1)]
        public float m_FaceShadowBlendFactor;
        [Range(0, 10)]
        public float m_OutlineThickness;

    }

序列化

    if (GUILayout.Button("保存序列化数据"))
            {
                var assetPath = EditorPrefs.GetString(m_PahtKey);
                var p = string.IsNullOrEmpty(assetPath) ? "Assets/RawResource" : assetPath;
                string path = EditorUtility.SaveFilePanelInProject("保存序列化数据", $"VirtualSun.asset", "asset", "选择要保存的位置", p);
                EditorPrefs.SetString(m_PahtKey, path);

                var source = target as VirtualSunMono;
                var data = CreateInstance();

                data.m_Mode = source.m_Mode;
                data.m_AloneChar = source.m_AloneChar;
                data.m_CharList = source.m_CharList;
                data.m_AloneList = source.m_AloneList;
                data.m_MainColor = source.m_MainColor;
                data.m_RampColor = source.m_RampColor;
                data.m_RampHDRColor = source.m_RampHDRColor;
                data.m_OutlineColor = source.m_OutlineColor;
                data.m_AsyncFaceRamp = source.m_AsyncFaceRamp;
                data.m_LightInfo = source.m_LightInfo;
                data.m_Metallic = source.m_Metallic;
                data.m_HighLightLineStrenth = source.m_HighLightLineStrenth;
                data.m_MetallicAtten = source.m_MetallicAtten;
                data.m_FaceShadowBlendFactor = source.m_FaceShadowBlendFactor;
                data.m_OutlineThickness = source.m_OutlineThickness;

                if (string.IsNullOrEmpty(path))
                    return;

                AssetDatabase.CreateAsset(data, path);
                AssetDatabase.SaveAssets();
                EditorUtility.FocusProjectWindow();
                Selection.activeObject = data;
            }

反序列化

if (GUILayout.Button("读取序列化数据"))
            {
                var assetPath = EditorPrefs.GetString(m_PahtKey);
                var p = string.IsNullOrEmpty(assetPath) ? "Assets/RawResource" : assetPath;
                string path = EditorUtility.OpenFilePanelWithFilters("选择要载入数据", p, new string[] { "asset", "asset" });
                if (string.IsNullOrEmpty(path))
                    return;
                path = path.Substring(path.IndexOf("Assets/"));
                EditorPrefs.SetString(m_PahtKey, path);

                var data = AssetDatabase.LoadAssetAtPath(path);

                serializedObject.FindProperty("m_Mode").enumValueIndex = (int)data.m_Mode;
                serializedObject.FindProperty("m_AloneChar").intValue = data.m_AloneChar;
                serializedObject.FindProperty("m_MainColor").colorValue = data.m_MainColor;
                serializedObject.FindProperty("m_RampColor").colorValue = data.m_RampColor;
                serializedObject.FindProperty("m_RampHDRColor").colorValue = data.m_RampHDRColor;
                serializedObject.FindProperty("m_AsyncFaceRamp").boolValue = data.m_AsyncFaceRamp;
                serializedObject.FindProperty("m_FaceShadowBlendFactor").floatValue = data.m_FaceShadowBlendFactor;
                serializedObject.FindProperty("m_OutlineColor").colorValue = data.m_OutlineColor;
                serializedObject.FindProperty("m_OutlineThickness").floatValue = data.m_OutlineThickness;
                serializedObject.FindProperty("m_LightInfo").vector3Value = data.m_LightInfo;
                serializedObject.FindProperty("m_Metallic").floatValue = data.m_Metallic;
                serializedObject.FindProperty("m_HighLightStrength").floatValue = data.m_HighLightLineStrenth;
                serializedObject.FindProperty("m_MetallicAtten").floatValue = data.m_MetallicAtten;
                serializedObject.FindProperty("m_HighLightLineStrenth").floatValue = data.m_HighLightLineStrenth;

                serializedObject.ApplyModifiedProperties();
            }

你可能感兴趣的:(ScriptableObject 序列化使用案例)