The SoloAnimation
component is only used to play a single animation so it will automatically apply the first frame of that animation in Edit Mode as soon as it is assigned (as long as the Apply In Edit Mode
toggle is enabled):
This is implemented by using the MonoBehaviour.OnValidate
message which gets called in Edit Mode whenever an instance of the script is loaded or a value is changed in the Inspector. So if you want your own scripts to work like that too, you can simply use it to call either of the following methods:
AnimancerUtilities.EditModeSampleAnimation
applies the first frame of the animation (or a specific time if you use the optional parameter).AnimancerUtilities.EditModePlay
actually plays the animation.
They're both AnimationClip
Extension Methods so they can be called directly on the animation:
[SerializeField]
private AnimancerComponent _Animancer;
[SerializeField]
private AnimationClip _Idle;
private void OnValidate()
{
// Apply the first frame of the animation.
_Idle.EditModeSampleAnimation(_Animancer);
// Apply the effects of the frame 0.5 seconds into the animation.
_Idle.EditModeSampleAnimation(_Animancer, 0.5f);
// Actually play the animation.
_Idle.EditModePlay(_Animancer);
}
- This is especially convenient for
Sprite
animations like those in the Directional Basics example so that you can simply apply the first frame of whatever Idle animation you assign to each character so that it will show the correctSprite
without you needing to also set theSprite
manually. - The Doors example allows each door to start anywhere between open and closed, so it uses this feature to show exactly where every door will be in the Scene view without needing to enter Play Mode.
Both of those methods automatically take care of a few extra concerns which wouldn't normally be relevant in a regular call to AnimancerComponent.Play
:
- You can actually pass in any
Component
and it will find the correct object to play the animation on by searching the parents and children of the given object. - They null-check the
AnimationClip
and the object to play it on so you don't have to worry about that. - They have
[Conditional("UNITY_EDITOR")]
attributes. - They do nothing in Play Mode. Otherwise changing anything in the script's Inspector in Play Mode would play the specified Idle animation and interrupt whatever Animancer was supposed to be doing.
- Also note that animations in Edit Mode do not count as modifications to the scene that will flag it as dirty (to tell Unity that it needs to be saved). This generally doesn't matter since any time the scene is opened the animation will simply be played again. However, when the scene does get saved it could be at any point in the animation and save whatever values it currently has, which would show up as modifications in a version control system.