I have created an empty GameObject and assigned it a script: Main.cs.
public class Main
{
[MenuItem("Custom workflow/Initialize")]
static void Initialize()
{
// Place a text mesh on the scene
var obj = new GameObject("Sample");
obj.transform.position = new Vector3(0, 0, 0);
var text = obj.AddComponent<TextMesh>();
text.text = "Hello world";
AnimationClip clip1 = CreateAnimationClip();
AnimatorController sm = CreateSimpleAnimatorController(clip1);
var animator = obj.AddComponent<Animator>();
animator.runtimeAnimatorController = sm;
animator.applyRootMotion = true;
}
private static AnimationClip CreateAnimationClip()
{
var translateX = AnimationCurve.Linear(0.0f, 0.0f, 2.0f, 25.0f);
var animationClip = new AnimationClip();
animationClip.SetCurve("", typeof(Transform), "position.x", translateX);
AssetDatabase.CreateAsset(animationClip, "Assets/procanim.anim");
return animationClip;
}
private static AnimatorController CreateSimpleAnimatorController(
Motion motion1 = null, Motion motion2 = null)
{
// Creates the controller
var controller = AnimatorController.CreateAnimatorControllerAtPath(
"Assets/SimpleStateMachineTransitions.controller");
// Add parameters
controller.AddParameter("GoToB", AnimatorControllerParameterType.Bool);
controller.AddParameter("GoToA", AnimatorControllerParameterType.Bool);
// Add StateMachines
var rootStateMachine = controller.layers[0].stateMachine;
// Add States
var stateA1 = rootStateMachine.AddState("stateA1");
stateA1.motion = motion1;
var stateB1 = rootStateMachine.AddState("stateB1");
stateB1.motion = motion2;
// Add Transitions
var transitionAB = stateA1.AddTransition(stateB1);
transitionAB.AddCondition(AnimatorConditionMode.If, 0, "GoToB");
transitionAB.duration = 0;
var transitionBA = stateB1.AddTransition(stateA1);
transitionBA.AddCondition(AnimatorConditionMode.If, 0, "GoToA");
transitionBA.duration = 0;
return controller;
}
}
The problem
As I invoke the menu item, I get all assets in place. When I hit play, nothing happens. I can see state stateA1 is on, but the animation does not run. If I click on the text object in the scene...
...this is what I see in the animation window:
I think the problem might be here:
animationClip.SetCurve("", typeof(Transform), "position.x", translateX);
Because, when I remove the code generate Transform.position property from the animation timeline and manually add one via UI, it works once I hit play again.

