0
\$\begingroup\$

In unity we can create parameters in the animator tab to help to control changes in animation. I'm able to create this parameters in the unity itself and manipulate them in the C# script, but I want to know how I can do all this process only through code. So I would like to create a parameter and set the condition in the animation, only through coding. How can I do that?

\$\endgroup\$
2
  • \$\begingroup\$ Why would you want to do that? Do you want to create an editor script to automatize the creation of animation controllers? If you want to do that while the game is running: I don't think you can do that and I really think you shouldn't, because there is surely a better solution for whatever problem you are trying to solve. \$\endgroup\$
    – Philipp
    Commented Oct 21, 2021 at 13:55
  • \$\begingroup\$ I'm a begginer, so it's not a problem and I don't pretend to do this, It's just a once in a life experience that I want to try \$\endgroup\$
    – Roman
    Commented Oct 21, 2021 at 14:09

1 Answer 1

2
\$\begingroup\$

Poke around in the Unity API documentation and you'll find the UnityEditor.Animations namespace, containing AnimatorController.AddParameter.

Here's an example from the AnimatorController docs showing how to use this to create a custom animator controller with specific parameters and state transitions via script:

using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
using System.Collections;

// Create a menu item that causes a new controller and statemachine to be created.

public class SM : MonoBehaviour
{
    [MenuItem("MyMenu/Create Controller")]
    static void CreateController()
    {
        // Creates the controller
        var controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath("Assets/Mecanim/StateMachineTransitions.controller");

        // Add parameters
        controller.AddParameter("TransitionNow", AnimatorControllerParameterType.Trigger);
        controller.AddParameter("Reset", AnimatorControllerParameterType.Trigger);
        controller.AddParameter("GotoB1", AnimatorControllerParameterType.Trigger);
        controller.AddParameter("GotoC", AnimatorControllerParameterType.Trigger);

        // Add StateMachines
        var rootStateMachine = controller.layers[0].stateMachine;
        var stateMachineA = rootStateMachine.AddStateMachine("smA");
        var stateMachineB = rootStateMachine.AddStateMachine("smB");
        var stateMachineC = stateMachineB.AddStateMachine("smC");

        // Add States
        var stateA1 = stateMachineA.AddState("stateA1");
        var stateB1 = stateMachineB.AddState("stateB1");
        var stateB2 = stateMachineB.AddState("stateB2");
        stateMachineC.AddState("stateC1");
        var stateC2 = stateMachineC.AddState("stateC2"); // don’t add an entry transition, should entry to state by default

        // Add Transitions
        var exitTransition = stateA1.AddExitTransition();
        exitTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "TransitionNow");
        exitTransition.duration = 0;

        var resetTransition = rootStateMachine.AddAnyStateTransition(stateA1);
        resetTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "Reset");
        resetTransition.duration = 0;

        var transitionB1 = stateMachineB.AddEntryTransition(stateB1);
        transitionB1.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "GotoB1");
        stateMachineB.AddEntryTransition(stateB2);
        stateMachineC.defaultState = stateC2;
        var exitTransitionC2 = stateC2.AddExitTransition();
        exitTransitionC2.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "TransitionNow");
        exitTransitionC2.duration = 0;

        var stateMachineTransition = rootStateMachine.AddStateMachineTransition(stateMachineA, stateMachineC);
        stateMachineTransition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, "GotoC");
        rootStateMachine.AddStateMachineTransition(stateMachineA, stateMachineB);
    }
}

I've never used this API, but was able to find it in about a minute of searching, so don't underestimate your ability to answer your own curiosity with a little legwork.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .