- LocalizationManager: 新增翻译表(SetFormatted/SetDuration/LocalizeOperateMsg) - LoginPanel: InputField placeholder本地化、字体颜色保持 - HistoryPanel: 用时数据本地化、placeholder本地化 - RecordDetailPanel: 操作详情消息本地化(LanguageChanged重建) - AppraiseWindowBase: 评价等级本地化、操作消息重建 - EditorAppraiser: 所有评估消息改用InjectOperateMsgLocalized - StudentOperateRecorder: 新增InjectOperateMsgLocalized方法 - LocalizationLanguageTestBar: 单例模式、ScreenSpaceOverlay筛选 - 字体切换时保留颜色和verticalOverflow
107 lines
3.3 KiB
C#
107 lines
3.3 KiB
C#
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Runtime.InteropServices;
|
||
using System.Runtime.Serialization;
|
||
using System.Runtime.Serialization.Formatters.Binary;
|
||
using System.Text;
|
||
using UnityEngine;
|
||
|
||
public static class BytesHelper
|
||
{
|
||
//// <summary>
|
||
/// 结构体转byte数组
|
||
/// </summary>
|
||
/// <param name="structObj">要转换的结构体</param>
|
||
/// <returns>转换后的byte数组</returns>
|
||
public static byte[] StructToBytes<T>(T structObj) where T : struct
|
||
{
|
||
//得到结构体的大小
|
||
int size = Marshal.SizeOf(structObj);
|
||
//创建byte数组
|
||
byte[] bytes = new byte[size];
|
||
//分配结构体大小的内存空间
|
||
IntPtr structPtr = Marshal.AllocHGlobal(size);
|
||
//将结构体拷到分配好的内存空间
|
||
Marshal.StructureToPtr(structObj, structPtr, false);
|
||
//从内存空间拷到byte数组
|
||
Marshal.Copy(structPtr, bytes, 0, size);
|
||
//释放内存空间
|
||
Marshal.FreeHGlobal(structPtr);
|
||
//返回byte数组
|
||
return bytes;
|
||
}
|
||
|
||
/// <summary>
|
||
/// byte数组转结构体
|
||
/// </summary>
|
||
/// <param name="bytes">byte数组</param>
|
||
/// <param name="type">结构体类型</param>
|
||
/// <returns>转换后的结构体</returns>
|
||
public static T BytesToStuct<T>(byte[] bytes) where T :struct
|
||
{
|
||
//得到结构体的大小
|
||
int size = Marshal.SizeOf<T>();
|
||
//byte数组长度小于结构体的大小
|
||
if (size > bytes.Length)
|
||
{
|
||
//返回空
|
||
return default;
|
||
}
|
||
//分配结构体大小的内存空间
|
||
IntPtr structPtr = Marshal.AllocHGlobal(size);
|
||
//将byte数组拷到分配好的内存空间
|
||
Marshal.Copy(bytes, 0, structPtr, size);
|
||
//将内存空间转换为目标结构体
|
||
T obj = Marshal.PtrToStructure<T>(structPtr);
|
||
//释放内存空间
|
||
Marshal.FreeHGlobal(structPtr);
|
||
//返回结构体
|
||
return obj;
|
||
}
|
||
|
||
|
||
///// <summary>
|
||
///// 将一个object对象序列化,返回一个byte[]
|
||
///// </summary>
|
||
///// <param name="obj">能序列化的对象</param>
|
||
///// <returns></returns>
|
||
//public static byte[] ObjectToBytes(object obj)
|
||
//{
|
||
// if (obj == null) return null;
|
||
// using (MemoryStream ms = new MemoryStream())
|
||
// {
|
||
// IFormatter formatter = new BinaryFormatter();
|
||
// formatter.Serialize(ms, obj);
|
||
// return ms.GetBuffer();
|
||
// }
|
||
//}
|
||
|
||
|
||
///// <summary>
|
||
///// 将一个序列化后的byte[]数组还原
|
||
///// </summary>
|
||
///// <param name="Bytes"></param>
|
||
///// <returns></returns>
|
||
//public static object BytesToObject(byte[] Bytes)
|
||
//{
|
||
// using (MemoryStream ms = new MemoryStream(Bytes))
|
||
// {
|
||
// IFormatter formatter = new BinaryFormatter();
|
||
// return formatter.Deserialize(ms);
|
||
// }
|
||
//}
|
||
|
||
public static byte[] ClassToBytes<T>(T msg)
|
||
{
|
||
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg));
|
||
}
|
||
|
||
public static T BytesToClass<T>(byte[] bytes) where T : class
|
||
{
|
||
return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(bytes));
|
||
}
|
||
}
|