- Home /
Create new Mecanim parameter from code?
Hello!
Is anyone aware if it's possible to create new parameters for a Mecanim animation controller from code? Either by using an Editor script, or even better at runtime.
Thanks, David
Answer by DavidDebnar · Dec 23, 2015 at 03:55 PM
Ok, I solved it. The key is to cast the Runtime Animation Controller the Animator class provides to an AnimationController and then it's easy to add new parameters using the .AddParameter method.
(only possible in the Editor, because it uses UnityEditor.Animations)
using UnityEngine;
using UnityEditor.Animations;
public class ParameterTest : MonoBehaviour
{
protected void AddParameter ()
{
// We first need to get the animator attached to this object
// and then cast it's runtimeAnimationController
// to an AnimationController
Animator animator = GetComponent<Animator> ();
AnimatorController animatorController = (AnimatorController) animator.runtimeAnimatorController;
// Method 1, keeps reference to the parameter
AnimatorControllerParameter parameter = new AnimatorControllerParameter ();
parameter.type = AnimatorControllerParameterType.Bool;
parameter.name = "NewParameter1";
animatorController.AddParameter (parameter);
// Method 2, shorter, but without a reference
animatorController.AddParameter ("NewParameter2", AnimatorControllerParameterType.Float);
}
}
Your answer
Follow this Question
Related Questions
Mecanim Animation Parameter Types: Boolean vs. Trigger 3 Answers
How to customize an Animator Controller? 0 Answers
Skills controlled by animation (event callbacks and parameters) 0 Answers
Can the mecanim animators Controller var be set in code? 1 Answer
Using Override Animation Controllers (NOT WORKING) 0 Answers