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

【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即可!

相关文章:

  • YOLO模型predict(预测/推理)的参数设置
  • Java集合框架解析:从基础到底层源码
  • 题目练习之综合运用
  • 【PhysUnits】4.4 零类型(Z0)及其算术运算(zero.rs)
  • 《解锁具身智能社交密码:文化适配算法探秘》
  • 小目标检测层优化+多模态数据增强——YOLOv5在油气管道环焊缝缺陷识别的创新应用
  • SymPy | 隐函数导数求解:从基础到高阶的完整指南
  • 卓力达手撕垫片:精密制造的创新解决方案与多领域应用
  • docker运行Redis
  • 基于DeepSeek的智能客服系统实践与创新
  • 【Game】Powerful——Punch and Kick(12)
  • C+++STL(一)
  • Linux 内核等待机制详解:prepare_to_wait_exclusive 与 TASK_INTERRUPTIBLE
  • 反射操作注解的详细说明
  • 网络漏洞扫描系统都有着什么作用?
  • 图漾相机错误码解析(待补充)
  • 三种嵌入式开发常用的组网方式
  • Unity开发:预制体、接口与枚举
  • 基于tar包安装,创建两个tomcat实例
  • 测试自动化开发框架全解析
  • 财政部:4月份中央收入增长1.6%,今年以来首月实现正增长
  • 特朗普亲家有了新工作:美国驻法大使
  • 家国万里·时光故事会|从徐光启到徐家汇,一颗甘薯里的家国
  • 两次通话、三点诉求,泽连斯基对美称愿与俄签署和平备忘录
  • 荷兰外交大臣费尔德坎普将访华
  • 这个死亡率第一的“老年病”,正悄悄逼近年轻人