Is there any way to save an animation curve, such as using PlayerPrefs?
I have this script that records animations at runtime as animation curves, but the curves in the Inspector reset each time the script is changed (public variables added).
using UnityEngine; using System.Collections; public class curvRecord : MonoBehaviour { public AnimationCurve xPositionCurve; public float timer = 0; public float recorder = 0; public bool play; public bool Reset; public bool record; void Update () { if (Reset && !play && ! record) { xPositionCurve.RemoveKey (5); play = false; recorder = 0; timer = 0; } if(play && timer<=recorder){ record = false; Reset = false; Vector3 localPosition = transform.position; localPosition.x = xPositionCurve.Evaluate (timer); transform.localPosition = localPosition; // Increase the timer by the time since last frame timer += Time.deltaTime; } else if(!play && record) { Reset = false; recorder += Time.deltaTime; xPositionCurve.AddKey (recorder, gameObject.transform.position.x); } } } I can record and play my animation, but when I exit from the game it resets my curve animation. When I start game again, I want to load the last curve animation
You can see my problem in this video.
I have tried to use PlayerPrefs, but when I use it at runtime I have a noticeable lag in my game.
using UnityEngine; using System.Collections; public class curvRecord : MonoBehaviour { [SerializeField] public AnimationCurve xPositionCurve; public AnimationCurve yPositionCurve; public AnimationCurve zPositionCurve; public float timer = 0; public float recorder = 0; public bool record; public bool play; public bool Reset; public float[] CurveFrameValue; public bool OnceRunSaving; public bool OnceRunLoading; Keyframe[] ks = new Keyframe[500]; void Saver(){ for (int m = 0; m < CurveFrameValue.Length; m++) { CurveFrameValue [m] = xPositionCurve [m].value; PlayerPrefsX.SetFloatArray ("xpos", CurveFrameValue); print (m); } } // Update is called once per frame void Update () { if(OnceRunLoading){ for (int n = 0; n < CurveFrameValue.Length; n++) { CurveFrameValue = PlayerPrefsX.GetFloatArray ("xpos"); ks[n] = new Keyframe(n, CurveFrameValue[n]); xPositionCurve = new AnimationCurve(ks); } transform.position = new Vector3(Time.time, xPositionCurve.Evaluate(Time.time), 0); OnceRunLoading = false; } if (Reset && !play && !record) { xPositionCurve.RemoveKey (50); yPositionCurve.RemoveKey (50); zPositionCurve.RemoveKey (50); play = false; recorder = 0; timer = 0; } if(play && timer<=recorder){ record = false; Reset = false; Vector3 localPosition = transform.position; localPosition.x = xPositionCurve.Evaluate (timer); localPosition.y = yPositionCurve.Evaluate (timer); localPosition.z = zPositionCurve.Evaluate (timer); transform.localPosition = localPosition; // Increase the timer by the time since last frame timer += Time.deltaTime; } else if(!play && record) { timer = 0; Reset = false; recorder += Time.deltaTime; xPositionCurve.AddKey (recorder, gameObject.transform.position.x); yPositionCurve.AddKey (recorder, gameObject.transform.position.y); zPositionCurve.AddKey (recorder, gameObject.transform.position.z); if(!OnceRunSaving){ Saver (); OnceRunSaving = true; } }else if(play && timer>=recorder){ // Increase the timer by the time since last frame timer = 0; } else if(play && record) { Reset = true; } else if(Reset) { Reset = true; } } } 
