unityzgy/Assets/Scripts/PublicTool/XDictionary.cs
ayuan9957 bf12e02276 feat: 多语言本地化系统 - 支持中/英/法/俄实时切换
- LocalizationManager: 新增翻译表(SetFormatted/SetDuration/LocalizeOperateMsg)
- LoginPanel: InputField placeholder本地化、字体颜色保持
- HistoryPanel: 用时数据本地化、placeholder本地化
- RecordDetailPanel: 操作详情消息本地化(LanguageChanged重建)
- AppraiseWindowBase: 评价等级本地化、操作消息重建
- EditorAppraiser: 所有评估消息改用InjectOperateMsgLocalized
- StudentOperateRecorder: 新增InjectOperateMsgLocalized方法
- LocalizationLanguageTestBar: 单例模式、ScreenSpaceOverlay筛选
- 字体切换时保留颜色和verticalOverflow
2026-07-16 10:05:59 +08:00

169 lines
5.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using UnityEngine;
/// <summary>
/// 解决原生字典无法被序列化XML的问题
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
[Serializable]
public class XDictionary<TKey, TValue>: Dictionary<TKey, TValue>, IXmlSerializable, ISerializable
{
#region
#region + public DictionaryEx()
/// <summary>
/// 默认构造函数
/// </summary>
public XDictionary()
: base()
{
}
#endregion
#region + public DictionaryEx(int capacity)
/// <summary>
/// 构造函数
/// </summary>
/// <param name="capacity">可包含的初始元素数</param>
public XDictionary(int capacity)
: base(capacity)
{
}
#endregion
#region + public DictionaryEx(IEqualityComparer<TKey> comparer)
/// <summary>
/// 构造函数
/// </summary>
/// <param name="comparer">比较键时要使用的 比较器 实现,或者为 null以便为键类型使用默认的 比较器</param>
public XDictionary(IEqualityComparer<TKey> comparer)
: base(comparer)
{
}
#endregion
#region + public DictionaryEx(IDictionary<TKey, TValue> dictionary)
/// <summary>
/// 构造函数
/// </summary>
/// <param name="dictionary">初始数据</param>
public XDictionary(IDictionary<TKey, TValue> dictionary)
: base(dictionary)
{
}
#endregion
#region + public DictionaryEx(int capacity, IEqualityComparer<TKey> comparer)
/// <summary>
/// 构造函数
/// </summary>
/// <param name="capacity">可包含的初始元素数</param>
/// <param name="comparer">比较键时要使用的 比较器 实现,或者为 null以便为键类型使用默认的 比较器</param>
public XDictionary(int capacity, IEqualityComparer<TKey> comparer)
: base(capacity, comparer)
{
}
#endregion
#region + public DictionaryEx(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
/// <summary>
/// 构造函数
/// </summary>
/// <param name="dictionary">初始数据</param>
/// <param name="comparer">比较键时要使用的 比较器 实现,或者为 null以便为键类型使用默认的 比较器</param>
public XDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
: base(dictionary, comparer)
{
}
#endregion
//二进制反序列化构造函数
public XDictionary(SerializationInfo info, StreamingContext context):base(info, context)
{
}
#endregion
#region + public XmlSchema GetSchema()
/// <summary>
/// 取得概要
/// 注根据MSDN的文档此方法为保留方法一定返回 null。
/// </summary>
/// <returns>Xml概要</returns>
public XmlSchema GetSchema()
{
return null;
}
#endregion
#region XML + public void ReadXml(XmlReader reader)
/// <summary>
/// 从 XML 对象中反序列化生成本对象
/// </summary>
/// <param name="reader">包含反序列化对象的 XmlReader 流</param>
public void ReadXml(XmlReader reader)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
bool wasEmpty = reader.IsEmptyElement;
reader.Read();
if (wasEmpty) return;
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.ReadStartElement("Item");
reader.ReadStartElement("Key");
TKey key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement("Value");
TValue value = (TValue)valueSerializer.Deserialize(reader);
reader.ReadEndElement();
this.Add(key, value);
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
#endregion
#region XML + public void WriteXml(XmlWriter writer)
/// <summary>
/// 将本对象序列化为 XML 对象
/// </summary>
/// <param name="writer">待写入的 XmlWriter 对象</param>
public void WriteXml(XmlWriter writer)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
foreach (TKey key in this.Keys)
{
writer.WriteStartElement("Item");
writer.WriteStartElement("Key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteStartElement("Value");
TValue value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
#endregion
}