namespace Marro.PacManUdon { using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor.Animations; using UnityEditor; public class AnimationRecorder : MonoBehaviour { [SerializeField] AnimationClip clip; [SerializeField] GameObject root; [SerializeField] GameObject[] gameObjectsToAnimate; private GameObjectRecorder recorder; void Start() { recorder = new GameObjectRecorder(root); foreach (GameObject gameObject in gameObjectsToAnimate) { // if(gameObject.GetComponent() || gameObject.GetComponent()) // { // recorder.BindComponentsOfType(gameObject, true); // } // recorder.BindComponentsOfType(gameObject, true); string path = AnimationUtility.CalculateTransformPath(gameObject.transform, root.transform); recorder.Bind(EditorCurveBinding.FloatCurve(path, typeof(GameObject), "m_IsActive")); Pellet pellet = gameObject.GetComponent(); if (pellet) { recorder.Bind(EditorCurveBinding.FloatCurve(path, typeof(SpriteRenderer), "m_Enabled")); if (pellet.isPowerPellet) { recorder.Bind(EditorCurveBinding.PPtrCurve(path, typeof(SpriteRenderer), "m_Sprite")); } continue; } if (gameObject.GetComponent()) { recorder.Bind(EditorCurveBinding.FloatCurve(path, typeof(SpriteRenderer), "m_Enabled")); recorder.Bind(EditorCurveBinding.PPtrCurve(path, typeof(SpriteRenderer), "m_Sprite")); } else if (gameObject.GetComponent()) { recorder.Bind(EditorCurveBinding.DiscreteCurve(path, typeof(MeshRenderer), "m_Enabled")); } recorder.Bind(EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalPosition.x")); recorder.Bind(EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalPosition.y")); recorder.Bind(EditorCurveBinding.FloatCurve(path, typeof(Transform), "m_LocalPosition.z")); } EditorCurveBinding[] bindings = recorder.GetBindings(); foreach (EditorCurveBinding binding in bindings) { Debug.Log($"{binding.path}, {binding.propertyName}, {binding.type}"); } } private static string GetGameObjectPathToObject(GameObject obj, Transform target) { string path = "/" + obj.name; while (!obj.transform.parent.Equals(target)) { obj = obj.transform.parent.gameObject; path = "/" + obj.name + path; } Debug.Log($"GetGameObjectPathToTransform from {obj} to {target.gameObject} gives {path}"); return path; } void LateUpdate() { if (clip == null) return; recorder.TakeSnapshot(Time.deltaTime); } void OnDisable() { if (clip == null) return; if (recorder.isRecording) { recorder.SaveToClip(clip); RemoveUnneededCurves(clip); } } private static void RemoveUnneededCurves(AnimationClip clip) { // Collect curves to process EditorCurveBinding[] bindings = AnimationUtility.GetCurveBindings(clip); List unneededCurves = new List(); foreach (var binding in bindings) { AnimationCurve curve = AnimationUtility.GetEditorCurve(clip, binding); if (curve == null || curve.keys.Length <= 2) { unneededCurves.Add(binding); } } // Remove unchanged curves foreach (var binding in unneededCurves) { AnimationUtility.SetEditorCurve(clip, binding, null); Debug.Log($"Removed unchanged curve for property: {binding.propertyName}"); } } } }