Advertisement
NovusX

animation_recorder

May 24th, 2021
1,404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.Animations;
  4.  
  5. public class AnimationRecorder : MonoBehaviour
  6. {
  7.     GameObjectRecorder _recorder;
  8.  
  9.     #region Inspector Variables
  10.  
  11.     [Tooltip("Must start with Assets/")]
  12.     [SerializeField] string _saveFolderLocation = "Assets/";
  13.  
  14.     [SerializeField] string _clipName;
  15.     [SerializeField] float _frameRate = 15f;
  16.  
  17.     [Header("Key Bindings")]
  18.     [SerializeField] string _startRecKey = "i";
  19.     [SerializeField] string _stopRecKey = "o";
  20.     [SerializeField] string _deleteRecKey = "p";
  21.     [Tooltip("ONLY USE WHEN ALL RELATED ASSETS ARE DELETED FROM ASSETS FOLDER")]
  22.     [SerializeField] string _deleteIndexKey = "l";
  23.  
  24.     #endregion
  25.  
  26.     private AnimationClip _clip;
  27.     private AnimationClip _currentClip;
  28.     private bool _canRecord = true;
  29.     private int _index;
  30.     private string _currentClipName;
  31.  
  32.     private void OnEnable()
  33.     {
  34.         if(_clip == null)
  35.         {
  36.             CreateNewClip();
  37.         }
  38.  
  39.         var savedIndex = PlayerPrefs.GetInt(gameObject.name + "Index");
  40.  
  41.         if ( savedIndex != 0)
  42.         {
  43.             _index = savedIndex;
  44.         }
  45.     }
  46.  
  47.     void Start()
  48.     {
  49.         _recorder = new GameObjectRecorder(gameObject);
  50.         _recorder.BindComponentsOfType<Transform>(gameObject, true);
  51.  
  52.         if (_clipName == "")
  53.         {
  54.             _clipName = gameObject.name + "_animation";
  55.         }
  56.  
  57.     }
  58.  
  59.     private void Update()
  60.     {
  61.         ControllerInputs();
  62.     }
  63.  
  64.     private void ControllerInputs()
  65.     {
  66.         if (Input.GetKeyDown(_startRecKey))
  67.         {
  68.             StartRecording();
  69.         }
  70.  
  71.         if (Input.GetKeyDown(_stopRecKey))
  72.         {
  73.             StopRecording();
  74.         }
  75.  
  76.         if (Input.GetKeyDown(_deleteRecKey))
  77.         {
  78.             DeleteRecording();
  79.         }
  80.  
  81.         //reset index if all clips have been deleted from the assets
  82.         //ONLY USE IF ALL ASSETS HAVE BEEN DELETED
  83.         if (Input.GetKey(_deleteIndexKey))
  84.         {
  85.             PlayerPrefs.DeleteKey(gameObject.name + "Index");
  86.             Debug.LogWarning("Clip name indexing has been reset");
  87.             _index = 0;
  88.         }
  89.     }
  90.  
  91.     private void StartRecording()
  92.     {
  93.         _canRecord = true;
  94.         CreateNewClip();
  95.         Debug.Log("Animation Recording for " + gameObject.name + " has STARTED");
  96.     }
  97.  
  98.     private void StopRecording()
  99.     {
  100.         Debug.Log("Animation Recording for " + gameObject.name + " has STOPPED");
  101.  
  102.         _canRecord = false;
  103.  
  104.         _recorder.SaveToClip(_currentClip);
  105.  
  106.         AssetDatabase.CreateAsset(_currentClip, _saveFolderLocation + _currentClipName + ".anim");
  107.  
  108.         AssetDatabase.SaveAssets();
  109.     }
  110.  
  111.     private void DeleteRecording()
  112.     {
  113.         if (_canRecord)
  114.         {
  115.             Debug.LogWarning("Cannot delete when recording!");
  116.             return;
  117.         }
  118.  
  119.         if (!AssetDatabase.Contains(_currentClip))
  120.         {
  121.             Debug.LogWarning("Clip Has not been saved yet.");
  122.             return;
  123.         }
  124.         AssetDatabase.DeleteAsset(_saveFolderLocation + _currentClipName + ".anim");
  125.         Debug.Log("Clip has been DELETED");
  126.    
  127.     }
  128.  
  129.     private void LateUpdate()
  130.     {
  131.         if (_clip == null) return;
  132.  
  133.         if(_canRecord)
  134.         {
  135.             _recorder.TakeSnapshot(Time.deltaTime);
  136.         }
  137.     }
  138.  
  139.     private void CreateNewClip()
  140.     {
  141.         _clip = new AnimationClip();
  142.  
  143.         if (_clip.name.Contains(_clip.name))
  144.         {
  145.             _clip.name = _clipName + " " + (_index++);
  146.             _currentClipName = _clip.name;
  147.         }
  148.  
  149.         _clip.frameRate = _frameRate;
  150.  
  151.         _currentClip = _clip;
  152.     }
  153.  
  154.     private void OnDisable()
  155.     {
  156.         PlayerPrefs.SetInt(gameObject.name + "Index", _index);
  157.     }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement