unity json解析IPA后续

以前说到的,有很大的限制,只能解析简单的类,如果复杂的就会有问题,从老外哪里看到一片博客,是将类中的list   等复杂对象序列化,

 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

// List<T>
[Serializable]
public class Serialization<T>
{
    [SerializeField]
    List<T> target;
    public List<T> ToList() { return target; }

    public Serialization(List<T> target)
    {
        this.target = target;
    }
}

// Dictionary<TKey, TValue>
[Serializable]
public class Serialization<TKey, TValue> : ISerializationCallbackReceiver
{
    [SerializeField]
    List<TKey> keys;
    [SerializeField]
    List<TValue> values;

    Dictionary<TKey, TValue> target;
    public Dictionary<TKey, TValue> ToDictionary() { return target; }

    public Serialization(Dictionary<TKey, TValue> target)
    {
        this.target = target;
    }

    public void OnBeforeSerialize()
    {
        keys = new List<TKey>(target.Keys);
        values = new List<TValue>(target.Values);
    }

    public void OnAfterDeserialize()
    {
        var count = Math.Min(keys.Count, values.Count);
        target = new Dictionary<TKey, TValue>(count);
        for (var i = 0; i < count; ++i)
        {
            target.Add(keys[i], values[i]);
        }
    }
}

// BitArray
[Serializable]
public class SerializationBitArray : ISerializationCallbackReceiver
{
    [SerializeField]
    string flags;

    BitArray target;
    public BitArray ToBitArray() { return target; }

    public SerializationBitArray(BitArray target)
    {
        this.target = target;
    }

    public void OnBeforeSerialize()
    {
        var ss = new System.Text.StringBuilder(target.Length);
        for (var i = 0; i < target.Length; ++i)
        {
            ss.Insert(0, target[i] ? '1' : '0');
        }
        flags = ss.ToString();
    }

    public void OnAfterDeserialize()
    {
        target = new BitArray(flags.Length);
        for (var i = 0; i < flags.Length; ++i)
        {
            target.Set(flags.Length - i - 1, flags[i] == '1');
        }
    }
}

 

posted @ 2017-11-25 14:51  飞天艾特小猪  阅读(266)  评论(0编辑  收藏  举报