CreateAsset for AnimatorController doesn't save StateMachine

I’m creating an AnimatorController, Layer, States, AnimationClips all from code. It all works perfectly… until I save the scene and all of my states and layers leak away and are collected.

I’ve been trying to find out how to force them to be saved as well?
Currently I create an asset for the controller and for each clip, as well as saving a prefab of my 2D skeleton.

Is there a way to force the layers and the states to also be saved along with the controller?

Use the bottom method.