Is there any way to save an animation curve by playerprefs ? The animation curves in the inspector reset each time the script is changed (public variables added)
I want record my animation In runtime as animation curve then I load It next time(same playerprefs I want save/load it)
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 curve animation but when I go exit from game reset curve animation.when I start game again I want load last curve animation
You can see my problem in this video:
videoLink
I have to use playerprefsX but when I use It in runtime I have lag In my game
please tell me what Is easiest way?!?!?! for saving animation curves
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 Start () {
}
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;
}
}
}