Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Help: Use GameObjectRecorder to record multiple new animation clips

Discussion in 'Animation' started by jhuh3226, Jul 27, 2021.

  1. jhuh3226

    jhuh3226

    Joined:
    Oct 24, 2018
    Posts:
    8
    Hi, I'm seeking ways to record multiple new animation clips using GameObjectRecorder.

    I have created an array to create multiple animations, expecting to have results that all start from 0.0f. To be specific, I want to have new recordings of clip0, clip1, clip2.

    However, the result of the following code yields stacking animation. The "stacking" means, clip1 as combined animation of both clip0 and clip1. Clip2 also has a combined animation of clip1 followed by clip0, and clip2 followed by clip1. The photo below will give you more idea of what is happening.

    (clip0 - 7:30 seconds)
    Screen Shot 2021-07-27 at 4.49.53 PM.png
    (clip1 - 19:00 seconds/ starts recording clip1 after recording clip0)
    Screen Shot 2021-07-27 at 4.50.05 PM.png
    (clip2- 26:00 seconds/ starts recording clip1 after recording clip0 & clip1)
    Screen Shot 2021-07-27 at 4.50.14 PM.png


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor.Animations;
    5.  
    6. public class HierarchyRecoder : MonoBehaviour
    7. {
    8.     // The clip the recording is going to be saved to.
    9.     public AnimationClip[] char0clips;
    10.     private int char0clipNum;
    11.  
    12.     // Checkbox to start/stop the recording.
    13.     public bool record = false;
    14.     public bool startedRecording = false;
    15.  
    16.     // The main feature: the actual recorder.
    17.     private GameObjectRecorder m_Recorder;
    18.  
    19.     public GameObject BodyTrackingManger, Replay;
    20.  
    21.     void Start()
    22.     {
    23.         char0clipNum = -1;
    24.  
    25.         // Create the GameObjectRecorder.
    26.         m_Recorder = new GameObjectRecorder(gameObject);
    27.  
    28.         // Set it up to record the transforms recursively.
    29.         m_Recorder.BindComponentsOfType<Transform>(gameObject, true);
    30.     }
    31.  
    32.     // The recording needs to be done in LateUpdate in order
    33.     // to be done once everything has been updated
    34.     // (animations, physics, scripts, etc.).
    35.     void LateUpdate()
    36.     {
    37.         if (char0clips == null)
    38.             return;
    39.  
    40.         if (record)
    41.         {
    42.             // As long as "record" is on: take a snapshot.
    43.             m_Recorder.TakeSnapshot(Time.deltaTime);
    44.         }
    45.         else if (!record && startedRecording)
    46.         {
    47.             Debug.Log("end recording");
    48.  
    49.             // save to clip
    50.             m_Recorder.SaveToClip(char0clips[char0clipNum]);
    51.  
    52.             // clear recording.
    53.             // m_Recorder.ResetRecording();
    54.  
    55.             startedRecording = false;
    56.         }
    57.     }
    58.  
    59.     public void Record()
    60.     {
    61.         record = true;
    62.         startedRecording = true;
    63.         print("Clicked recording BT");
    64.  
    65.         char0clipNum++;
    66.     }
    67.  
    68.     public void StopRecording()
    69.     {
    70.         record = false;
    71.     }
    72. }
    I have commented out "ResetRecording" as it yields the following error.
    InvalidOperationException: Cannot save to clip as there is nothing to save. The method TakeSnapshot() has not been called.


    I'd appreciate help to solve this.
    Again, my goal is to record fresh/ new recordings by clicking the new record/stop button.
     
  2. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    179
    You are using 1 recorder for 3 clips, but you should have 3 recorders (one for each clip) so that you get 3 output files.
     
  3. jhuh3226

    jhuh3226

    Joined:
    Oct 24, 2018
    Posts:
    8
    Thank you did solve continuous recording.

    I am encountering an issue with the following. It does not stop me from recording, though I'd like to find a solution to this. Do you have an idea about how to solve this?
    Down below is my code, making arrays of recorders. I have assigned them in editor.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor.Animations;
    5.  
    6. public class HierarchyRecoder : MonoBehaviour
    7. {
    8.     // The clip the recording is going to be saved to.
    9.     public AnimationClip[] char0clips;
    10.     private int char0clipNum;
    11.     public Animation anim;
    12.  
    13.     // Checkbox to start/stop the recording.
    14.     public bool record = false;
    15.     public bool startedRecording = false;
    16.  
    17.     // The main feature: the actual recorder.
    18.     public GameObjectRecorder[] m_Recorders;
    19.  
    20.     public GameObject BodyTrackingManger, Replay;
    21.  
    22.     // Replay recorded animation
    23.     public RuntimeAnimatorController UserCreatedAniPlayer;
    24.  
    25.     void Start()
    26.     {
    27.         char0clipNum = -1;
    28.  
    29.         // Create the GameObjectRecorder.
    30.         for (int i = 0; i < 4; i++)
    31.         {
    32.             m_Recorders[i] = new GameObjectRecorder(gameObject);
    33.  
    34.             // Set it up to record the transforms recursively.
    35.             m_Recorders[i].BindComponentsOfType<Transform>(gameObject, true);
    36.         }
    37.     }
    38.  
    39.     void LateUpdate()
    40.     {
    41.  
    42.         // print(record);
    43.  
    44.         if (char0clips == null)
    45.             return;
    46.  
    47.         if (record)
    48.         {
    49.             m_Recorders[char0clipNum].TakeSnapshot(Time.deltaTime);
    50.  
    51.         }
    52.  
    53.         else if (!record && startedRecording)
    54.         {
    55.             Debug.Log("end recording");
    56.  
    57.             // "record" is off, but we were recording:
    58.             // save to clip
    59.             m_Recorders[char0clipNum].SaveToClip(char0clips[char0clipNum]);
    60.  
    61.             // clear recording.
    62.             //m_Recorder.ResetRecording();
    63.  
    64.             startedRecording = false;
    65.         }
    66.     }
    67.  
    68.     public void Record()
    69.     {
    70.         record = true;
    71.         startedRecording = true;
    72.         print("Clicked recording BT");
    73.  
    74.         // everytime new recording starts, create new clip and recorder
    75.         char0clipNum++;
    76.     }
    77.  
    78.     public void StopRecording()
    79.     {
    80.         Debug.Log("Stopped recording movement of 'Char0' and saved to clip " + char0clipNum);
    81.  
    82.         record = false;
    83.     }
    84. }
    I am also searching for help on finding the recorder that can be built in iOS device as the gameObjectRecorder is only available in Unity editor. For more information, I am using ARfoundation to rig off characters and try to make an animation recorder where users can record and replay their animated characters. I've looked into BVH, which is not possible to implement in iOS built. Is storing each selection's position of the rigged character in the array, and replaying them the only solution I can implement this?

    Thank you!
     
  4. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    179
    Try to initialize your array with the right size, in the declaration at line 18: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays

    I don't know how to help you with in-game recordings, let's hope other users can pitch in.
     
unityunity