I'm trying to fill my animation component automatically with clips which are dropped into a sister component. For nice UX I want the animation component just to sit there - artists shouldn't mess with it (HideFlags.NotEditable
).
But I also don't want to fill the animation component during runtime if I can avoid it.
But there I run into a problem. Here's a little test code. Checking the toggle will add a test clip to the animation component's clip list, unchecking the toggle will remove it again:
[RequireComponent(typeof(Animation))]
public class OnValidateTest : MonoBehaviour
{
public AnimationClip TestClip;
public bool Toggle;
private void OnValidate()
{
Debug.Log("OnValidate");
Animation animationComponent = GetComponent<Animation>();
// Reset clip list in Animation component, also eliminating empty clips (only clean way I found)
UnityEditor.AnimationUtility.SetAnimationClips(animationComponent, new AnimationClip[0]);
if (Toggle)
{
animationComponent.AddClip(TestClip, "test");
}
}
}
The problem now:
- It works in the scene view
- It works in prefab mode
- But when trying to edit the asset by clicking on the prefab asset in the project tab I'll run into trouble
Problem 1: OnValidate
will be called infinitely. (constant asset updating)
Problem 2: Clip assignment will fail and the inspector will display "type mismatch" in the array - (like I tried to assign an instance instead of an asset??)
Here's also a video showing the behavior.
Is what I want achievable ?