// // /*=============================================================================== // // Copyright (C) 2024 PhantomsXR Ltd. All Rights Reserved. // // // // This file is part of the Phantom.XRMOD.UnityFusion.Runtime.CodeHook. // // // // The XR-MOD cannot be copied, distributed, or made available to // // third-parties for commercial purposes without written permission of PhantomsXR Ltd. // // // // Contact nswell@phantomsxr.com for licensing requests. // // ===============================================================================*/ using UnityEngine; namespace Phantom.XRMOD.UnityFusion.Runtime.CodeHook { /// /// Represents a serializable version of the Unity . /// This is used to store curve data in a way that can be easily serialized (e.g., to JSON). /// [System.Serializable] public class AnimationCurveData { public KeyframeData[] keys; public WrapMode preWrapMode; public WrapMode postWrapMode; /// /// Represents the data for a single keyframe in the animation curve. /// [System.Serializable] public class KeyframeData { /// The time of the keyframe. public float time; /// The value of the curve at this time. public float value; /// The incoming tangent. public float inTangent; /// The outgoing tangent. public float outTangent; /// The tangent mode (auto, linear, constant, etc.). public int tangentMode; } /// /// Initializes a new instance of from a Unity . /// /// The source Unity animation curve. public AnimationCurveData(AnimationCurve _curve) { keys = new KeyframeData[_curve.keys.Length]; for (int tmp_I = 0; tmp_I < _curve.keys.Length; tmp_I++) { var tmp_Key = _curve.keys[tmp_I]; keys[tmp_I] = new KeyframeData { time = tmp_Key.time, value = tmp_Key.value, inTangent = tmp_Key.inTangent, outTangent = tmp_Key.outTangent, tangentMode = tmp_Key.tangentMode }; } preWrapMode = _curve.preWrapMode; postWrapMode = _curve.postWrapMode; } } /// /// Extension methods for serializing and deserializing animation curves. /// public static class Extensions { /// /// Serializes an into a JSON string. /// /// The curve to serialize. /// A JSON string representation of the curve. public static string SerializeAnimationCurve(this AnimationCurve _curve) { AnimationCurveData tmp_CurveData = new AnimationCurveData(_curve); return JsonUtility.ToJson(tmp_CurveData); } /// /// Deserializes a JSON string into an . /// /// The JSON string representing the curve. /// A new instance. public static AnimationCurve DeserializeAnimationCurve(this string _json) { AnimationCurveData tmp_CurveData = JsonUtility.FromJson(_json); Keyframe[] tmp_Keys = new Keyframe[tmp_CurveData.keys.Length]; for (int tmp_I = 0; tmp_I < tmp_CurveData.keys.Length; tmp_I++) { var tmp_KeyData = tmp_CurveData.keys[tmp_I]; tmp_Keys[tmp_I] = new Keyframe( tmp_KeyData.time, tmp_KeyData.value, tmp_KeyData.inTangent, tmp_KeyData.outTangent ); tmp_Keys[tmp_I].tangentMode = tmp_KeyData.tangentMode; } AnimationCurve tmp_Curve = new AnimationCurve(tmp_Keys) { preWrapMode = tmp_CurveData.preWrapMode, postWrapMode = tmp_CurveData.postWrapMode }; return tmp_Curve; } } }