Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Saving runtime animation Unity3d

Discussion in 'Scripting' started by unity_user_12345, Jul 27, 2015.

  1. unity_user_12345

    unity_user_12345

    Joined:
    May 21, 2015
    Posts:
    14
    I have a .fbx file and I matched it with a Mecanim. During the run time I modify some of the joints rotations, and I wonder how I can save the whole animation on a file to include the modifications I did to the animation in the runtime.

    Below is what I did but this saves the original animation not the modified one, so if anyone could please advise how I can save the animation including the modifications made in the runtime.

    Code (csharp):
    1.  
    2. AnimationClip[] myclips = UnityEditor.AnimationUtility.GetAnimationClips (Mygameobject);
    3. if (myclips .Length > 0) {
    4. folder = EditorUtility.SaveFolderPanel ("Save my clip", "", "");
    5. for (int i=0; i<myclips .Length; i++) {
    6. AnimationClip myclip = myclips ;
    7. progress = ((float)myclips.Length) / (float)i;
    8. EditorUtility.DisplayProgressBar ("Saving Animation", myclip .name, progress);
    9. ParseClip (myclip , i);
    10. }
    11. EditorUtility.ClearProgressBar ();
    12. }
    13.  
    14. void ParseClip (AnimationClip clip, int clipnumber) {
    15. AnimationClipCurveData[] cdataarray = AnimationUtility.GetAllCurves (clip, true);
    16. int l = ((AnimationClipCurveData[])cdataarray).Length;
    17. for(int x=0;x<l;x++)
    18. {
    19. AnimationClipCurveData cdata = cdataarray [x];
    20. s.Append(cdata.propertyName+".value");
    21. }
    22. for (int i=0; i<cdataarray[0].curve.keys.Length; i++)
    23. {
    24. for(int x=0;x<l;x++)
    25. {
    26. AnimationClipCurveData cdata = cdataarray [x];
    27. Keyframe kf = cdata.curve.keys;
    28. s.Append(kf.value);
    29. }
    30. }
    31. FileStream f = new FileStream ("My_file.txt", FileMode.OpenOrCreate, FileAccess.Write);
    32. StreamWriter sw = new StreamWriter (f);
    33. sw.Write (sb.ToString ());
    34. sw.Close ();
    35. f.Close ();
    36. }
    37.  
     
    Last edited: Jul 27, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    can you please use [code ][/code ] tags when pasting code into the forums, really helps with the readability. There is a sticky on them at the top of the scripting forum.
     
  3. unity_user_12345

    unity_user_12345

    Joined:
    May 21, 2015
    Posts:
    14
    Done :)
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    what are "s" and "sb"?
     
  5. unity_user_12345

    unity_user_12345

    Joined:
    May 21, 2015
    Posts:
    14
    stringbuilder
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    well ParseClip does a bunch of appending to "s", then writes "sb" to the file...