diff --git a/Runtime/ColorJsonConverter.cs b/Runtime/ColorJsonConverter.cs new file mode 100644 index 0000000..8954b75 --- /dev/null +++ b/Runtime/ColorJsonConverter.cs @@ -0,0 +1,87 @@ +using Newtonsoft.Json; +using UnityEngine; +using System; + +// ReSharper disable once CheckNamespace + +namespace GameLovers +{ + /// + /// JSON converter for Unity's Color struct that handles serialization to/from: + /// - Hex color strings (e.g. "#FF0000FF" for red) + /// - RGBA object format (e.g. {"r":1,"g":0,"b":0,"a":1}) + /// + /// + /// This converter enables proper serialization of Unity Color objects with Newtonsoft.Json. + /// It's particularly useful for saving color data in JSON configuration files or network payloads. + /// + public class ColorJsonConverter : JsonConverter + { + /// + /// Reads JSON data and converts it to a Unity Color object + /// + /// JSON reader providing the input data + /// Type of object to deserialize (should be Color) + /// Existing value of the object being read + /// Whether there is an existing value + /// JSON serializer instance + /// Deserialized Color object + /// + /// Supports both hex color strings and RGBA object formats: + /// - "#RRGGBBAA" or "#RRGGBB" (missing alpha defaults to 1) + /// - {"r":1,"g":0,"b":0,"a":1} (missing components default to 0, except alpha which defaults to 1) + /// + public override Color ReadJson(JsonReader reader, Type objectType, Color existingValue, bool hasExistingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.String) + { + string colorString = reader.Value.ToString(); + Color color; + if (ColorUtility.TryParseHtmlString(colorString, out color)) + { + return color; + } + } + else if (reader.TokenType == JsonToken.StartObject) + { + float r = 0, g = 0, b = 0, a = 1; + + reader.Read(); + while (reader.TokenType != JsonToken.EndObject) + { + string propertyName = reader.Value.ToString().ToLower(); + reader.Read(); + + switch (propertyName) + { + case "r": r = Convert.ToSingle(reader.Value); break; + case "g": g = Convert.ToSingle(reader.Value); break; + case "b": b = Convert.ToSingle(reader.Value); break; + case "a": a = Convert.ToSingle(reader.Value); break; + } + + reader.Read(); + } + + return new Color(r, g, b, a); + } + + return Color.white; + } + + /// + /// Writes a Color object to JSON format + /// + /// JSON writer for output + /// Color value to serialize + /// JSON serializer instance + /// + /// Always serializes to hex string format (e.g. "#FF0000FF" for red) + /// This provides a compact and web-friendly representation + /// + public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer) + { + writer.WriteValue("#" + ColorUtility.ToHtmlStringRGBA(value)); + } + } +} \ No newline at end of file diff --git a/Runtime/ColorJsonConverter.cs.meta b/Runtime/ColorJsonConverter.cs.meta new file mode 100644 index 0000000..6a5ce6d --- /dev/null +++ b/Runtime/ColorJsonConverter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6fa436c37e1210f4391f1a832f674f3f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: