Tanoda
pb_MatrixConverter.cs
Go to the documentation of this file.
1using UnityEngine;
2using Newtonsoft.Json;
3using Newtonsoft.Json.Serialization;
4using Newtonsoft.Json.Linq;
5using System.Reflection;
6using System;
7using System.Linq;
8
10{
11 public class pb_MatrixConverter : JsonConverter
12 {
13 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
14 {
15 JObject o = new JObject();
16
17 o.Add("$type", value.GetType().AssemblyQualifiedName);
18
19 Matrix4x4 matrix = (UnityEngine.Matrix4x4)value;
20
21 o.Add("00", matrix[0,0]);
22 o.Add("01", matrix[0,1]);
23 o.Add("02", matrix[0,2]);
24 o.Add("03", matrix[0,3]);
25
26 o.Add("10", matrix[1,0]);
27 o.Add("11", matrix[1,1]);
28 o.Add("12", matrix[1,2]);
29 o.Add("13", matrix[1,3]);
30
31 o.Add("20", matrix[2,0]);
32 o.Add("21", matrix[2,1]);
33 o.Add("22", matrix[2,2]);
34 o.Add("23", matrix[2,3]);
35
36 o.Add("30", matrix[3,0]);
37 o.Add("31", matrix[3,1]);
38 o.Add("32", matrix[3,2]);
39 o.Add("33", matrix[3,3]);
40
41 o.WriteTo(writer, serializer.Converters.ToArray());
42 }
43
44 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
45 {
46 JObject o = JObject.Load(reader);
47
48 Matrix4x4 matrix = Matrix4x4.identity;
49
50 matrix[0,0] = (float) o.GetValue("00");
51 matrix[0,1] = (float) o.GetValue("01");
52 matrix[0,2] = (float) o.GetValue("02");
53 matrix[0,3] = (float) o.GetValue("03");
54
55 matrix[1,0] = (float) o.GetValue("10");
56 matrix[1,1] = (float) o.GetValue("11");
57 matrix[1,2] = (float) o.GetValue("12");
58 matrix[1,3] = (float) o.GetValue("13");
59
60 matrix[2,0] = (float) o.GetValue("20");
61 matrix[2,1] = (float) o.GetValue("21");
62 matrix[2,2] = (float) o.GetValue("22");
63 matrix[2,3] = (float) o.GetValue("23");
64
65 matrix[3,0] = (float) o.GetValue("30");
66 matrix[3,1] = (float) o.GetValue("31");
67 matrix[3,2] = (float) o.GetValue("32");
68 matrix[3,3] = (float) o.GetValue("33");
69
70 return matrix;
71 }
72
73 public override bool CanConvert(Type objectType)
74 {
75 return typeof(UnityEngine.Matrix4x4).IsAssignableFrom(objectType);
76 }
77 }
78}
override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
override bool CanConvert(Type objectType)
override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)