0

I want to save the animation clip file i recorded using scripting during play mode, i want to be able to use the animation clip file after canceling the play Mode,

Badr Douah
  • 63
  • 13
  • It appears that someone may have already addressed this [here](https://stackoverflow.com/questions/44865782/how-to-record-the-screen-in-unity-and-make-output-as-a-file). – Kevin Richer Sep 11 '19 at 00:32
  • @MellowMammoth ... your link is about a screen (image/video) recording and has nothing to do with recording and storing audio... – derHugo Sep 11 '19 at 05:38

1 Answers1

2

Here's a script I wrote which enables you to record the transform component of the gameObject this script is attached to and save it as an animation clip in Assets folder. I excluded the localScale for brevity reasons but you can easily add it.

To summarize:

Check the documentation page of AnimationClip.SetCurve() for further information about how to animate children of a gameObject and finding out property names.

P.S. static int clipCount prevents you from overwriting the same clip in a play session, you can record multiple clips. However clips you record in a previous session will be overwritten if you don't rename them becase clipCount variable will be reset.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

class TransformClip {
    AnimationCurve[] positionCurves;
    AnimationCurve[] rotationCurves;
    int curveCount = 3;
    static int clipCount = 1;

    public TransformClip() {
        positionCurves = new AnimationCurve[curveCount];
        rotationCurves = new AnimationCurve[curveCount];

        for (int i = 0; i < curveCount; i++) {
            positionCurves[i] = new AnimationCurve();
            rotationCurves[i] = new AnimationCurve();
        }
    }

    public void record(Transform transform, float recordTime) {
        float keyTime = Time.time - recordTime;
        for (int i = 0; i < curveCount; i++) {
            positionCurves[i].AddKey(keyTime, transform.position[i]);
            rotationCurves[i].AddKey(keyTime, transform.eulerAngles[i]);
        }
    }

    public AnimationClip stop() {
        AnimationClip clip = new AnimationClip();
        clip.SetCurve("", typeof(Transform), "localPosition.x", positionCurves[0]);
        clip.SetCurve("", typeof(Transform), "localPosition.y", positionCurves[1]);
        clip.SetCurve("", typeof(Transform), "localPosition.z", positionCurves[2]);
        clip.SetCurve("", typeof(Transform), "localRotation.x", rotationCurves[0]);
        clip.SetCurve("", typeof(Transform), "localRotation.y", rotationCurves[1]);
        clip.SetCurve("", typeof(Transform), "localRotation.z", rotationCurves[2]);
        return clip;
    }

    public void saveAnimation(AnimationClip clip, string clipName) {
        clipName = "Assets/" + clipName + " - " + clipCount + ".anim";
        AssetDatabase.CreateAsset(clip, clipName);
        AssetDatabase.SaveAssets();
        clipCount++;
    }
};

public class Record : MonoBehaviour {
    public bool isRecording;

    float recordStartTime;
    TransformClip transformClip;

    void Update() {

        if (Input.GetKeyDown(KeyCode.R) && !isRecording) {
            // Start recording.
            recordStartTime = Time.time;
            isRecording = true;
            transformClip = new TransformClip();
        }

        else if (Input.GetKeyDown(KeyCode.R) && isRecording) {
            // Stop recording.
            isRecording = false;
            AnimationClip clip = transformClip.stop();
            transformClip.saveAnimation(clip, "runtimeClip");
        }

        if (isRecording) {
            transformClip.record(transform, recordStartTime);
        }
    }
}
  • 1
    Thank you very much for the code and explanation, i've done the recording part the only issue i faced is how to save the animation clip during runtime, the solution you provided works perfectly for editor based stuff but not runtime, but that is okey for my case thank you very much for your time – Badr Douah Sep 12 '19 at 20:31
  • 1
    You're welcome. I thought you were going to use it inside Unity3D not the build because it says "after canceling the play mode." in the question. Maybe you should clarify that because I have never thought this would be for the build. Anyways, it was a fun coding challenge for me. Cheers. – madbuggerswall Sep 12 '19 at 21:43