Add: Gradient adapter

This commit is contained in:
AnnulusGames 2024-02-14 16:50:09 +09:00
parent ad604ec334
commit d375e6c54f
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,74 @@
#if ALCHEMY_SUPPORT_SERIALIZATION
using Unity.Serialization.Json;
using UnityEngine;
namespace Alchemy.Serialization.Internal
{
partial class AlchemyJsonAdapter : IJsonAdapter<Gradient>, IJsonAdapter<GradientColorKey>, IJsonAdapter<GradientAlphaKey>
{
public Gradient Deserialize(in JsonDeserializationContext<Gradient> context)
{
if (context.SerializedValue.IsNull()) return null;
var view = context.SerializedValue.AsObjectView();
var gradient = new Gradient();
if (view.TryGetMember("colorKeys", out var colorKeys)) gradient.colorKeys = context.DeserializeValue<GradientColorKey[]>(colorKeys.Value());
if (view.TryGetMember("alphaKeys", out var alphaKeys)) gradient.alphaKeys = context.DeserializeValue<GradientAlphaKey[]>(alphaKeys.Value());
if (view.TryGetMember("mode", out var mode)) gradient.mode = context.DeserializeValue<GradientMode>(mode.Value());
return gradient;
}
public GradientColorKey Deserialize(in JsonDeserializationContext<GradientColorKey> context)
{
var colorKey = default(GradientColorKey);
var color = context.SerializedValue.AsObjectView().GetMember("color").Value();
colorKey.color = context.DeserializeValue<Color>(color);
var time = context.SerializedValue.AsObjectView().GetMember("time").Value();
colorKey.time = time.AsFloat();
return colorKey;
}
public GradientAlphaKey Deserialize(in JsonDeserializationContext<GradientAlphaKey> context)
{
var alphaKey = default(GradientAlphaKey);
var alpha = context.SerializedValue.AsObjectView().GetMember("alpha").Value();
alphaKey.alpha = alpha.AsFloat();
var time = context.SerializedValue.AsObjectView().GetMember("time").Value();
alphaKey.time = time.AsFloat();
return alphaKey;
}
public void Serialize(in JsonSerializationContext<Gradient> context, Gradient value)
{
if (value == null)
{
context.Writer.WriteNull();
return;
}
context.Writer.WriteBeginObject();
context.SerializeValue("colorKeys", value.colorKeys);
context.SerializeValue("alphaKeys", value.alphaKeys);
context.SerializeValue("mode", value.mode);
context.Writer.WriteEndObject();
}
public void Serialize(in JsonSerializationContext<GradientColorKey> context, GradientColorKey value)
{
context.Writer.WriteBeginObject();
context.SerializeValue("color", value.color);
context.SerializeValue("time", value.time);
context.Writer.WriteEndObject();
}
public void Serialize(in JsonSerializationContext<GradientAlphaKey> context, GradientAlphaKey value)
{
context.Writer.WriteBeginObject();
context.SerializeValue("alpha", value.alpha);
context.SerializeValue("time", value.time);
context.Writer.WriteEndObject();
}
}
}
#endif

View File

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