Эх сурвалжийг харах

同步框架:修复内置unity类型不能用litjson序列化的问题

guodong 3 жил өмнө
parent
commit
58f04c3f96

+ 2 - 1
GameClient/Assets/Game/Launcher/GameLauncher.cs

@@ -3,7 +3,6 @@ using GFGGame;
 using ET;
 using System.Threading;
 using GFGGame.Launcher;
-using System;
 
 public class GameLauncher : MonoBehaviour
 {
@@ -19,6 +18,8 @@ public class GameLauncher : MonoBehaviour
 
         DontDestroyOnLoad(gameObject);
 
+        LitJson.UnityTypeBindings.Register();
+
         ETTask.ExceptionHandler += Log.Error;
 
         Log.ILog = new UnityLogger();

+ 40 - 0
GameClient/Assets/ThirdParty/LitJson/Extensions.cs

@@ -0,0 +1,40 @@
+using System;
+
+namespace LitJson.Extensions {
+
+    /// <summary>
+    /// 拓展方法
+    /// </summary>
+	public static class Extensions {
+
+		public static void WriteProperty(this JsonWriter w,string name,long value){
+			w.WritePropertyName(name);
+			w.Write(value);
+		}
+		
+		public static void WriteProperty(this JsonWriter w,string name,string value){
+			w.WritePropertyName(name);
+			w.Write(value);
+		}
+		
+		public static void WriteProperty(this JsonWriter w,string name,bool value){
+			w.WritePropertyName(name);
+			w.Write(value);
+		}
+		
+		public static void WriteProperty(this JsonWriter w,string name,double value){
+			w.WritePropertyName(name);
+			w.Write(value);
+		}
+
+	}
+
+    /// <summary>
+    /// 跳过序列化的标签
+    /// </summary>
+    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
+    public sealed class JsonIgnore : Attribute
+    {
+
+    }
+}

+ 11 - 0
GameClient/Assets/ThirdParty/LitJson/Extensions.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 375fea2d63470574e9d753d6935171e8
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 143 - 0
GameClient/Assets/ThirdParty/LitJson/UnityTypeBindings.cs

@@ -0,0 +1,143 @@
+using System;
+using LitJson.Extensions;
+
+namespace LitJson
+{
+    /// <summary>
+    /// Unity内建类型拓展
+    /// </summary>
+#if UNITY_EDITOR
+    [UnityEditor.InitializeOnLoad]
+#endif
+    public static class UnityTypeBindings
+    {
+        static UnityTypeBindings()
+        {
+            Register();
+        }
+
+        public static void Register()
+        {
+            // 注册Type类型的Exporter
+            JsonMapper.RegisterExporter<Type>((v, w) =>
+            {
+                w.Write(v.FullName);
+            });
+
+            JsonMapper.RegisterImporter<string, Type>((s) =>
+            {
+                return Type.GetType(s);
+            });
+            
+            // 注册Vector2类型的Exporter
+            Action<UnityEngine.Vector2, JsonWriter> writeVector2 = (v, w) =>
+            {
+                w.WriteObjectStart();
+                w.WriteProperty("x", v.x);
+                w.WriteProperty("y", v.y);
+                w.WriteObjectEnd();
+            };
+
+            JsonMapper.RegisterExporter<UnityEngine.Vector2>((v, w) =>
+            {
+                writeVector2(v, w);
+            });
+
+            // 注册Vector3类型的Exporter
+            Action<UnityEngine.Vector3, JsonWriter> writeVector3 = (v, w) =>
+            {
+                w.WriteObjectStart();
+                w.WriteProperty("x", v.x);
+                w.WriteProperty("y", v.y);
+                w.WriteProperty("z", v.z);
+                w.WriteObjectEnd();
+            };
+
+            JsonMapper.RegisterExporter<UnityEngine.Vector3>((v, w) =>
+            {
+                writeVector3(v, w);
+            });
+
+            // 注册Vector4类型的Exporter
+            JsonMapper.RegisterExporter<UnityEngine.Vector4>((v, w) =>
+            {
+                w.WriteObjectStart();
+                w.WriteProperty("x", v.x);
+                w.WriteProperty("y", v.y);
+                w.WriteProperty("z", v.z);
+                w.WriteProperty("w", v.w);
+                w.WriteObjectEnd();
+            });
+
+            // 注册Quaternion类型的Exporter
+            JsonMapper.RegisterExporter<UnityEngine.Quaternion>((v, w) =>
+            {
+                w.WriteObjectStart();
+                w.WriteProperty("x", v.x);
+                w.WriteProperty("y", v.y);
+                w.WriteProperty("z", v.z);
+                w.WriteProperty("w", v.w);
+                w.WriteObjectEnd();
+            });
+
+            // 注册Color类型的Exporter
+            JsonMapper.RegisterExporter<UnityEngine.Color>((v, w) =>
+            {
+                w.WriteObjectStart();
+                w.WriteProperty("r", v.r);
+                w.WriteProperty("g", v.g);
+                w.WriteProperty("b", v.b);
+                w.WriteProperty("a", v.a);
+                w.WriteObjectEnd();
+            });
+
+            // 注册Color32类型的Exporter
+            JsonMapper.RegisterExporter<UnityEngine.Color32>((v, w) =>
+            {
+                w.WriteObjectStart();
+                w.WriteProperty("r", v.r);
+                w.WriteProperty("g", v.g);
+                w.WriteProperty("b", v.b);
+                w.WriteProperty("a", v.a);
+                w.WriteObjectEnd();
+            });
+
+            // 注册Bounds类型的Exporter
+            JsonMapper.RegisterExporter<UnityEngine.Bounds>((v, w) =>
+            {
+                w.WriteObjectStart();
+
+                w.WritePropertyName("center");
+                writeVector3(v.center, w);
+
+                w.WritePropertyName("size");
+                writeVector3(v.size, w);
+
+                w.WriteObjectEnd();
+            });
+
+            // 注册Rect类型的Exporter
+            JsonMapper.RegisterExporter<UnityEngine.Rect>((v, w) =>
+            {
+                w.WriteObjectStart();
+                w.WriteProperty("x", v.x);
+                w.WriteProperty("y", v.y);
+                w.WriteProperty("width", v.width);
+                w.WriteProperty("height", v.height);
+                w.WriteObjectEnd();
+            });
+
+            // 注册RectOffset类型的Exporter
+            JsonMapper.RegisterExporter<UnityEngine.RectOffset>((v, w) =>
+            {
+                w.WriteObjectStart();
+                w.WriteProperty("top", v.top);
+                w.WriteProperty("left", v.left);
+                w.WriteProperty("bottom", v.bottom);
+                w.WriteProperty("right", v.right);
+                w.WriteObjectEnd();
+            });
+        }
+
+    }
+}

+ 11 - 0
GameClient/Assets/ThirdParty/LitJson/UnityTypeBindings.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: f127a589d0f45ec4bb223bb418674d84
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: