Last active
March 11, 2022 05:03
-
-
Save tarocco/adf6d6547d5ab11292496d2e019d4bc3 to your computer and use it in GitHub Desktop.
Component to evaluate Unity Animator in-editor with the press of a button by calling Animator.Update(0f) exactly once. Useful for applying one frame of animation to an object, such as a model pose, and saving it permanently in the scene. Supports undo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
[RequireComponent(typeof(Animator))] | |
public class AnimatorEvaluate : MonoBehaviour | |
{ | |
} | |
#if UNITY_EDITOR | |
[CustomEditor(typeof(AnimatorEvaluate))] | |
public class AnimatorEvaluateEditor : Editor | |
{ | |
const string CallAnimatorUpdateButtonText = "Call Animator.Update(0f)"; | |
private GameObject _GameObject; | |
private Animator _Animator; | |
void OnEnable() | |
{ | |
_GameObject = (target as Behaviour).gameObject; | |
_Animator = _GameObject.GetComponent<Animator>(); | |
} | |
private static void PerformActionOnAllDescendants(Transform xform, Action<Transform> action) | |
{ | |
action(xform); | |
foreach (Transform child in xform) | |
PerformActionOnAllDescendants(child, action); | |
} | |
public override void OnInspectorGUI() | |
{ | |
if (GUILayout.Button(CallAnimatorUpdateButtonText)) | |
{ | |
PerformActionOnAllDescendants(_GameObject.transform, (xform) => | |
{ | |
Undo.RecordObject(xform.gameObject, CallAnimatorUpdateButtonText); | |
}); | |
_Animator.Update(0f); | |
PerformActionOnAllDescendants(_GameObject.transform, (xform) => | |
{ | |
if (PrefabUtility.IsPartOfAnyPrefab(xform.gameObject)) | |
PrefabUtility.RecordPrefabInstancePropertyModifications(xform.gameObject); | |
}); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment