实现Unity对Dictionary的序列化
若有尝试过想在unity的inspector检视面板中像List或者数组那样可以编辑Dictionary变量的童鞋应该知道,Dictionary变量不会出现在inspector中,unity并不会直接序列化Dictionary类型,但实际上unity有提供接口使之可能:
unity doc: http://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.OnBeforeSerialize.html。
注意其中的This interface should be used very carefully. Unity's serializer usually runs on the non main thread, while most of the Unity API can only be called from the main thread.
请慎用该方法,它不是线程安全的。
using UnityEngine;
using System.Collections.Generic; /// Usage:
///
/// [System.Serializable]
/// class MyDictionary : SerializableDictionary<int, GameObject> {}
/// public MyDictionary dic;
///
[System.Serializable]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
{
// We save the keys and values in two lists because Unity does understand those.
[SerializeField]
private List<TKey> _keys;
[SerializeField]
private List<TValue> _values; // Before the serialization we fill these lists
public void OnBeforeSerialize()
{
//官方例子有误,去掉
} // After the serialization we create the dictionary from the two lists
public void OnAfterDeserialize()
{
this.Clear();
int count = Mathf.Min(_keys.Count, _values.Count);
for (int i = ; i < count; ++i)
{
this.Add(_keys[i], _values[i]);
}
}
}
实现Unity对Dictionary的序列化的更多相关文章
- Unity 用JSON库序列化与反序列化类,字典
Unity自己的json序列化是不支持字典格式的,但是 json .net库,功能很强大,还支持序列化字典. 下载地址:https://download.csdn.net/download/qq_15 ...
- Unity 3D 之通过序列化来存档游戏数据
我们在使用u3d开发一些单机游戏的过程中,都会涉及到游戏数据的存单和加载.一般情况下,如果存储的数据不复杂,我们就可以用PlayerPrefs,但有时涉及到的数据更加复杂,使用PlayerPrefs难 ...
- Unity 序列化 总结
查找了 Script Serialization http://docs.unity3d.com/Manual/script-Serialization.html 自定义序列化及例子: http:// ...
- ScriptableObject本地序列化后重启Unity后报The associated script can not be loaded.Please fix any compile errors and assign a valid script的坑
踩坑 做编辑器一些设置序列化存在本地的时候,继承自ScriptableObject的类通过 创建的asset文件. 在重启Unity后查看这个asset发现上面的所有序列化属性丢失,报的错就是 在不存 ...
- unity序列化
什么是序列化 unity的序列化在unity的开发中起着举重足轻的地位,许多核心的功能都是基于序列化和反序列化来实现的.序列化简单来讲就是就是将我们所要保存的数据进行二进制存储,然后当我们需要的时候, ...
- Unity 基于excel2json批处理读取Excel表并反序列化
excel2json是一款将Excel表格文件快速生成json和C#数据类的高效插件,详情了解如下: https://neil3d.github.io/coding/excel2json.html 该 ...
- 聊聊Unity项目管理的那些事:Git-flow和Unity
0x00 前言 目前所在的团队实行敏捷开发已经有了一段时间了.敏捷开发中重要的一个话题便是如何对项目进行恰当的版本管理.项目从最初使用svn到之后的Git One Track策略再到现在的GitFlo ...
- Unity -JsonUtility的使用
今天,为大家分享一下unity上的Json序列化,应该一说到这个词语,我们肯定会觉得,这应该是很常用的一个功能点:诚然,我们保存数据的时候,也许会用到json序列化,所以,我们有必要快速了解一下它的简 ...
- Unity的Attribute(特性)还算多吧
属性 (Attribute) 使用 Unity 的C#语言 ,利用属性(Attribute)来类定义和变量定义或区分其他的变量,您可以设置一种特殊行为.* 1 例如,您添加[SerializeFiel ...
随机推荐
- linux mint console-setup
sudo dpkg-reconfigure console-setup after setup, setupcon
- c# 文件IO操作 StreamReader StreamWriter Split 使用
StreamWriter(String,Boolean) 若要追加数据到该文件中,则为 true:若要覆盖该文件,则为 false. 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件. 例 ...
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-tha ...
- 总结:iview(基于vue.js的开源ui组件)学习的一些坑
1.要改变组件的样式 找到这个组件的class名,然后覆盖样式. 举例:修改select框,显示圆角.只需给找到类名并写样 .ivu-select-selection{ border-radius:1 ...
- LINQ 学习路程 -- 查询操作 Aggregate
聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...
- electron—Chromium有酒,Node有肉
谷歌V8引擎的出现,Node.js的诞生注定要把开发模式“搅乱”. 基于云应用,服务化,定制化的应用需求不断增加后使得传统的winform开发空间越来越小,而原来做前端的空间越来越大,Node.js ...
- python第四篇:linux命令行总结 + 自动备份Python程序
由于最近需要学习Python爬虫相关的知识,所以就先从Python基础.Linux基础开始进行了学习,下面主要是总结了常见的Linux的命令行.最后为了巩固学到的东西,尝试写了个自动备份的Python ...
- linux系统配置之开机启动过程(centos)
1.开机流程如下: 2.BIOS BIOS是英文"Basic Input Output System"的缩略词,直译过来后中文名称就是"基本输入输出系统".其实 ...
- name lookup of 'res' changed for new ISO 'res' scoping
#include<iostream> using namespace std; int pow ( int val, int exp ); int main() { int val = 2 ...
- c# ListBox
1. ListBox用法(http://www.cnblogs.com/chinahbzm/articles/3577285.html) 2. ListBox使用(http://www.cnblogs ...