Create Animator Controller via Editor Script?

Hello,

I’m trying to use an editor script to create myself a button that will instantiate an object and set it up the way I’d like. I’m adding an Animator component to the object, and would like to create a new Animator Controller for the object. Is this possible?

Thanks!

Above link is dead, but I did find precisely what I needed, again, in the Scripting Manual and I love unity inc! Unity - Scripting API: AnimatorController

I need to Necro this.

I showed it to Chat GPT and it generates any animations you want :smiley:

Chat GPT: Sure, here's an example script that creates an Animator Controller with Idle, Walk, and Run animations, and sets up transitions and parameters to trigger the animations.

using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;

public class CreatePlayerController : MonoBehaviour
{
    [MenuItem("MyMenu/Create Player Controller")]
    static void CreateController()
    {
        // Create the animator controller
        var controller = AnimatorController.CreateAnimatorControllerAtPath("Assets/PlayerController.controller");

        // Add parameters
        controller.AddParameter("IsWalking", AnimatorControllerParameterType.Bool);
        controller.AddParameter("IsRunning", AnimatorControllerParameterType.Bool);

        // Add states
        var idleState = controller.layers[0].stateMachine.AddState("Idle");
        var walkState = controller.layers[0].stateMachine.AddState("Walk");
        var runState = controller.layers[0].stateMachine.AddState("Run");

        // Add animations to the states
        var idleClip = AssetDatabase.LoadAssetAtPath<AnimationClip>("Assets/Animations/Idle.anim");
        var walkClip = AssetDatabase.LoadAssetAtPath<AnimationClip>("Assets/Animations/Walk.anim");
        var runClip = AssetDatabase.LoadAssetAtPath<AnimationClip>("Assets/Animations/Run.anim");

        idleState.motion = idleClip;
        walkState.motion = walkClip;
        runState.motion = runClip;

        // Add transitions between states
        var walkToIdle = walkState.AddTransition(idleState);
        walkToIdle.AddCondition(AnimatorConditionMode.IfNot, 0, "IsWalking");
        walkToIdle.duration = 0;

        var runToIdle = runState.AddTransition(idleState);
        runToIdle.AddCondition(AnimatorConditionMode.IfNot, 0, "IsRunning");
        runToIdle.duration = 0;

        var walkToRun = walkState.AddTransition(runState);
        walkToRun.AddCondition(AnimatorConditionMode.If, 0, "IsRunning");
        walkToRun.duration = 0;

        var runToWalk = runState.AddTransition(walkState);
        runToWalk.AddCondition(AnimatorConditionMode.If, 0, "IsWalking");
        runToWalk.duration = 0;

        var idleToWalk = idleState.AddTransition(walkState);
        idleToWalk.AddCondition(AnimatorConditionMode.If, 0, "IsWalking");
        idleToWalk.duration = 0;

        var idleToRun = idleState.AddTransition(runState);
        idleToRun.AddCondition(AnimatorConditionMode.If, 0, "IsRunning");
        idleToRun.duration = 0;
    }
}