Unity字典序列化,列表序列化

文章讲述了在Unity5.3中,如何使用JsonUtility对List和Dictionary进行序列化的问题以及解决方案,通过ISerializationCallbackReceiver实现前后序列化过程中的数据转换。
摘要由CSDN通过智能技术生成

Unity5.3从开始追加的JsonUtility,但是对于List 和Dictionary不能被直接序列化存储.

例如: 

数据模型:

using UnityEngine;  
using System;  
using System.Collections.Generic;  
  
[Serializable]  
public class Enemy  
{  
    [SerializeField]  
    string name;  
    [SerializeField]  
    List<string> skills;  
  
    public Enemy(string name, List<string> skills)  
    {  
        this.name = name;  
        this.skills = skills;  
    }  
}  

实际使用:

List<Enemy> enemies = new List<Enemy>();  
enemies.Add(new Enemy("怪物1", new List<string>() { "攻击" }));  
enemies.Add(new Enemy("怪物2", new List<string>() { "攻击", "恢复" }));  
Debug.Log(JsonUtility.ToJson(enemies)); 

输出为:{}

输出是没有任何的json字符串。

------------------------------------------------------------------------------------------------

在unity的官方网站,ISerializationCallbackReceiver继承的方法被提出,用的时候把此脚本放到项目里.

// Serialization.cs
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]);
        }
    }
}

使用:

// List<T> -> Json ( 例 : List<Enemy> )  
string str = JsonUtility.ToJson(new Serialization<Enemy>(enemies)); // 输出 : {"target":[{"name":"怪物1,"skills":["攻击"]},{"name":"怪物2","skills":["攻击","恢复"]}]}  
// Json-> List<T>  
List<Enemy> enemies = JsonUtility.FromJson<Serialization<Enemy>>(str).ToList();  
  
// Dictionary<TKey,TValue> -> Json( 例 : Dictionary<int, Enemy> )  
string str = JsonUtility.ToJson(new Serialization<int, Enemy>(enemies)); // 输出 : {"keys":[1000,2000],"values":[{"name":"怪物1","skills":["攻击"]},{"name":"怪物2","skills":["攻击","恢复"]}]}  
// Json -> Dictionary<TKey,TValue>  
Dictionary<int, Enemy> enemies = JsonUtility.FromJson<Serialization<int, Enemy>>(str).ToDictionary();  

转载自:https://www.cnblogs.com/Peng18233754457/p/8798731.html 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值