当前位置: 首页 > ai >正文

【Unity】Unity中将字典序列化

Unity中将字典序列化,在预制体上能够看到字典的内容,也可以直接在预制体上拖拽给字典赋值

直接上代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;public class SerializableDic
{
}[Serializable]
public class SerializableDic<TKey, TValue> :SerializableDic,ISerializationCallbackReceiver,IDictionary<TKey, TValue>{[SerializeField] private List<SerializableKeyValuePair> list = new List<SerializableKeyValuePair>();[Serializable]private struct SerializableKeyValuePair{public TKey Key;public TValue Value;public SerializableKeyValuePair(TKey key, TValue value){Key = key;Value = value;}}#region key indexprivate Dictionary<TKey, int> KeyPositions{get { return _keyPositions.Value; }}private Lazy<Dictionary<TKey, int>> _keyPositions;public SerializableDic(){_keyPositions = new Lazy<Dictionary<TKey, int>>(MakeKeyPositions);}#endregionprivate Dictionary<TKey, int> MakeKeyPositions(){Dictionary<TKey, int> dictionary = new Dictionary<TKey, int>(list.Count);for (var i = 0; i < list.Count; i++){dictionary[list[i].Key] = i;}return dictionary;}public void OnBeforeSerialize(){}public void OnAfterDeserialize(){_keyPositions = new Lazy<Dictionary<TKey, int>>(MakeKeyPositions);}#region IDictionary<TKey, TValue>/// <summary>///  array[index]   dic[key]/// </summary>/// <param name="key"></param>public TValue this[TKey key]{get => list[KeyPositions[key]].Value;set{SerializableKeyValuePair pair = new SerializableKeyValuePair(key, value);if (KeyPositions.ContainsKey(key)){list[KeyPositions[key]] = pair;}else{KeyPositions[key] = list.Count;list.Add(pair);}}}public ICollection<TKey> Keys{get { return list.Select(tuple => tuple.Key).ToArray(); }}public ICollection<TValue> Values{get { return list.Select(tuple => tuple.Value).ToArray(); }}public void Add(TKey key, TValue value){if (KeyPositions.ContainsKey(key))throw new ArgumentException("An element with the same key already exists in the dictionary.");else{KeyPositions[key] = list.Count;list.Add(new SerializableKeyValuePair(key, value));}}public bool ContainsKey(TKey key){return KeyPositions.ContainsKey(key);}public bool Remove(TKey key){if (KeyPositions.TryGetValue(key, out var index)){KeyPositions.Remove(key);list.RemoveAt(index);for (var i = index; i < list.Count; i++)KeyPositions[list[i].Key] = i;return true;}elsereturn false;}public bool TryGetValue(TKey key, out TValue value){if (KeyPositions.TryGetValue(key, out var index)){value = list[index].Value;return true;}else{value = default;return false;}}#endregion#region ICollection <KeyValuePair<TKey, TValue>>public int Count{get { return list.Count; }}public bool IsReadOnly{get { return false; }}public void Add(KeyValuePair<TKey, TValue> kvp){Add(kvp.Key, kvp.Value);}public void Clear(){list.Clear();}public bool Contains(KeyValuePair<TKey, TValue> kvp){return KeyPositions.ContainsKey(kvp.Key);}public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex){var numKeys = list.Count;if (array.Length - arrayIndex < numKeys)throw new ArgumentException("arrayIndex");for (var i = 0; i < numKeys; i++, arrayIndex++){var entry = list[i];array[arrayIndex] = new KeyValuePair<TKey, TValue>(entry.Key, entry.Value);}}public bool Remove(KeyValuePair<TKey, TValue> kvp){return Remove(kvp.Key);}#endregion#region IEnumerable <KeyValuePair<TKey, TValue>>public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator(){return list.Select(ToKeyValuePair).GetEnumerator();}static KeyValuePair<TKey, TValue> ToKeyValuePair(SerializableKeyValuePair skvp){return new KeyValuePair<TKey, TValue>(skvp.Key, skvp.Value);}IEnumerator IEnumerable.GetEnumerator(){return GetEnumerator();}#endregion
}

直接将该类替换为原字典类Dictionary即可!

http://www.xdnf.cn/news/7262.html

相关文章:

  • 为什么上传大量大文件推荐是使用 app 应用为不是 web 浏览器下载上传呢?
  • Java合并两个列表到目标列表,并且进行排序
  • 解决使用@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss“, timezone = “GMT+8“)时区转换无效的问题
  • leetcode3371. 识别数组中的最大异常值-medium
  • 软件架构之-论高并发下的可用性技术
  • 团队氛围紧张,如何提升工作积极性?
  • 交叉引用、多个参考文献插入、跨文献插入word/wps中之【插入[1-3]、连续文献】
  • 多类别异常检测新SOTA-MVMCAD
  • 中国城市间交通驾车距离矩阵(2024)
  • 人工智能、机器学习与深度学习:概念解析与内在联系
  • tcp/ip协议
  • Oracle buffer cache的内部结构
  • 国家互联网信息办公室关于发布第十一批深度合成服务算法备案信息的公告
  • POJ3107树的重心
  • 代码随想录算法训练营 Day51 图论Ⅱ岛屿问题Ⅰ
  • 开源模型应用落地-模型上下文协议(MCP)-Resource Template-资源模板的使用逻辑(六)
  • 【TTS回顾】深度剖析 TTS 合成效果的客观评估与主观评价
  • 星际争霸小程序:用Java实现策略模式的星际大战
  • 大模型在股骨干骨折诊疗全流程中的应用研究报告
  • 多卡跑ollama run deepseek-r1
  • DRIVEGPT4: 通过大语言模型实现可解释的端到端自动驾驶
  • 数据治理进阶:精读数据治理培训方案【附全文阅读】
  • 我用 CodeBuddy 打造了一个灵感收集应用 —— SparkNotes 开发实录
  • 一周快讯 | 银发文娱旅游一周新鲜事
  • 【日常笔记】wps如何将值转换成东西南北等风向汉字
  • python fastapi + react, 写一个图片 app
  • Cryosparc里头restack的妙用
  • Linux项目部署全攻略:从环境搭建到前后端部署实战
  • 计算机网络-HTTP与HTTPS
  • Java资源管理与防止泄漏:从SeaTunnel源码看资源释放