unity json解析IPA后续

Posted 飞天艾特小猪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        }
    }
}

 

以上是关于unity json解析IPA后续的主要内容,如果未能解决你的问题,请参考以下文章

Unity脚本生成ipa或apk的方法

unity怎么导入litjson

如何使用 Unity 解析 Json 文件

Unity使用JSONObject解析Json

在 c# (Unity) 中解析 JSON 文件

Unity:从远程源解析 JSON 时出错