Created
March 14, 2018 13:51
-
-
Save zcyemi/e0c71c4f8ba8a92944a17f888253db0d to your computer and use it in GitHub Desktop.
Json.Net.JsonConverter for Unity Vector struct.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using Newtonsoft.Json; | |
using System; | |
public class Vec4Conv : JsonConverter | |
{ | |
public override bool CanConvert(Type objectType) | |
{ | |
if (objectType == typeof(Vector4)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | |
{ | |
var t = serializer.Deserialize(reader); | |
var iv = JsonConvert.DeserializeObject<Vector4>(t.ToString()); | |
return iv; | |
} | |
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | |
{ | |
Vector4 v = (Vector4)value; | |
writer.WriteStartObject(); | |
writer.WritePropertyName("x"); | |
writer.WriteValue(v.x); | |
writer.WritePropertyName("y"); | |
writer.WriteValue(v.y); | |
writer.WritePropertyName("z"); | |
writer.WriteValue(v.z); | |
writer.WritePropertyName("w"); | |
writer.WriteValue(v.w); | |
writer.WriteEndObject(); | |
} | |
} | |
public class Vec3Conv : JsonConverter | |
{ | |
public override bool CanConvert(Type objectType) | |
{ | |
if (objectType == typeof(Vector3)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | |
{ | |
var t = serializer.Deserialize(reader); | |
var iv = JsonConvert.DeserializeObject<Vector3>(t.ToString()); | |
return iv; | |
} | |
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | |
{ | |
Vector3 v = (Vector3)value; | |
writer.WriteStartObject(); | |
writer.WritePropertyName("x"); | |
writer.WriteValue(v.x); | |
writer.WritePropertyName("y"); | |
writer.WriteValue(v.y); | |
writer.WritePropertyName("z"); | |
writer.WriteValue(v.z); | |
writer.WriteEndObject(); | |
} | |
} | |
public class Vec2Conv : JsonConverter | |
{ | |
public override bool CanConvert(Type objectType) | |
{ | |
if (objectType == typeof(Vector2)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | |
{ | |
var t = serializer.Deserialize(reader); | |
var iv = JsonConvert.DeserializeObject<Vector2>(t.ToString()); | |
return iv; | |
} | |
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | |
{ | |
Vector2 v = (Vector2)value; | |
writer.WriteStartObject(); | |
writer.WritePropertyName("x"); | |
writer.WriteValue(v.x); | |
writer.WritePropertyName("y"); | |
writer.WriteValue(v.y); | |
writer.WriteEndObject(); | |
} | |
} |
Thanks, nice example to start with.
Can this architecture or working method function with "PreserveReferencesHandling.All," which means I can choose to store the element once and have subsequent occurrences of the same instance be referenced using indexes in the JSON?
Thanks :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, thank you 😃