• 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');
            }
        }
    }
  • 相关阅读:
    前端安全-XSS攻击
    leetcode-0003 无重复字符的最长子串
    leetcode-0002 两数相加
    leetcode-0001 两数之和
    数据结构篇-数组(TypeScript版+Java版)
    前端性能优化(一)-- 文件的压缩与合并
    《深入浅出RxJS》读书笔记
    python工具函数
    [其他]Ubuntu安装genymotion后unable to load VirtualBox engine
    [linux]CentOS无法使用epel源
  • 原文地址:https://www.cnblogs.com/lzy575566/p/7753874.html
Copyright © 2020-2023  润新知