120 lines
4.4 KiB
C#
120 lines
4.4 KiB
C#
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<PacMan>() || gameObject.GetComponent<Ghost>())
|
|
// {
|
|
// recorder.BindComponentsOfType<Transform>(gameObject, true);
|
|
// }
|
|
// recorder.BindComponentsOfType<Renderer>(gameObject, true);
|
|
|
|
string path = AnimationUtility.CalculateTransformPath(gameObject.transform, root.transform);
|
|
|
|
recorder.Bind(EditorCurveBinding.FloatCurve(path, typeof(GameObject), "m_IsActive"));
|
|
|
|
Pellet pellet = gameObject.GetComponent<Pellet>();
|
|
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<SpriteRenderer>())
|
|
{
|
|
recorder.Bind(EditorCurveBinding.FloatCurve(path, typeof(SpriteRenderer), "m_Enabled"));
|
|
recorder.Bind(EditorCurveBinding.PPtrCurve(path, typeof(SpriteRenderer), "m_Sprite"));
|
|
}
|
|
else if (gameObject.GetComponent<MeshRenderer>())
|
|
{
|
|
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<EditorCurveBinding> unneededCurves = new List<EditorCurveBinding>();
|
|
|
|
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}");
|
|
}
|
|
}
|
|
}
|
|
} |