using MessagePack.Formatters; using System; using System.Collections.Generic; using UnityEngine; namespace MessagePack.Unity { public class UnityResolver : IFormatterResolver { public static IFormatterResolver Instance = new UnityResolver(); UnityResolver() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { formatter = (IMessagePackFormatter)UnityResolveryResolverGetFormatterHelper.GetFormatter(typeof(T)); } } } internal static class UnityResolveryResolverGetFormatterHelper { static readonly Dictionary formatterMap = new Dictionary() { {typeof(Vector2), new Vector2Formatter()}, {typeof(Vector3), new Vector3Formatter()}, {typeof(Vector4), new Vector4Formatter()}, {typeof(Quaternion), new QuaternionFormatter()}, {typeof(Color), new ColorFormatter()}, {typeof(Bounds), new BoundsFormatter()}, {typeof(Rect), new RectFormatter()}, {typeof(Vector2?), new StaticNullableFormatter(new Vector2Formatter())}, {typeof(Vector3?), new StaticNullableFormatter(new Vector3Formatter())}, {typeof(Vector4?), new StaticNullableFormatter(new Vector4Formatter())}, {typeof(Quaternion?),new StaticNullableFormatter(new QuaternionFormatter())}, {typeof(Color?),new StaticNullableFormatter(new ColorFormatter())}, {typeof(Bounds?),new StaticNullableFormatter(new BoundsFormatter())}, {typeof(Rect?),new StaticNullableFormatter(new RectFormatter())}, }; internal static object GetFormatter(Type t) { object formatter; if (formatterMap.TryGetValue(t, out formatter)) { return formatter; } return null; } } }