I need to change the Transition Duration per Runtime. I just didnt find a way to do access it. I found questions with similar problems though (links below)- those didnt work for me because they accessed it to change it via editor - script. But I need it in runtime. Is that even possible?
-
1\$\begingroup\$ afaik you can't change most of those values in code. (I'm sure for animation speed) I suggest you define multiple states with different durations. \$\endgroup\$– BizhanJan 16, 2017 at 13:54
-
\$\begingroup\$ animation speed CAN be changed - with a "multiplier". \$\endgroup\$– OC_RaizWJan 16, 2017 at 14:50
-
\$\begingroup\$ Oh! I see, I need to look into it. I hope you find your answer. \$\endgroup\$– BizhanJan 16, 2017 at 15:17
1 Answer
Alright so this was a difficult question that I had to do some thorough research on. To access the transition duration of a certain state you have to go through the animation controller, then find the right layer, then find the right state on that layer, then access the correct transition in that state. After all this you can then change the transition duration. Now you said you wanted the duration per runtime, in order to do anything per runtime in the animation controller I believe you would have to access the Animator class' runtimeAnimatorController. This class has limited abilities and you can not access even the layers, much less a state's transition duration so you have to use the "as" keyword to change it into a normal AnimatorController class. Now you can change the necessary values per runtime. The code would look something like this. (The majority of this code is to help you find the transition you wish to change)
void ChangeTransitionDuration(float value)
{
// Get a reference to the Animator Controller:
UnityEditor.Animations.AnimatorController ac = GetComponent<Animator>().runtimeAnimatorController as UnityEditor.Animations.AnimatorController;
//This part is IMPORTANT ^^
// Number of layers:
int layerCount = ac.layers.Length;
Debug.Log(string.Format("Layer Count: {0}", layerCount));
// Names of each layer:
for (int layer = 0; layer < layerCount; layer++)
{
Debug.Log(string.Format("Layer {0}: {1}", layer, ac.layers[layer].name));
}
// States on layer 0:
UnityEditor.Animations.AnimatorStateMachine sm = ac.layers[0].stateMachine;
UnityEditor.Animations.ChildAnimatorState[] states = sm.states;
foreach (UnityEditor.Animations.ChildAnimatorState s in states)
{
Debug.Log(string.Format("State: {0}", s.state.name));
}
// Access the duration variable to change it
ac.layers[desiredLayer].stateMachine.states[desiredState].state.transitions[desiredTransition].duration = desiredValue;
// You can split this up into different variables if you like
// I just wrote it all on one line so you could see the path to take
}
This code was taken and altered from TonyLi's answer here: http://answers.unity3d.com/questions/418854/getting-a-list-of-mecanim-states-in-animator.html
I hope this helps!
-
\$\begingroup\$ sir - you are awesome. i can update the question once its done. \$\endgroup\$– OC_RaizWJan 16, 2017 at 20:09
-
\$\begingroup\$ well it doesnt work for me :-/ Did you test this on a example project? \$\endgroup\$– OC_RaizWJan 17, 2017 at 10:49
-
\$\begingroup\$ I'm sorry it doesn't work for you. I have not tested it, but I did make sure there were no errors with the code. Did you make sure you found the correct layer, state, and transition? @OC_RaizW \$\endgroup\$– Sam RJan 18, 2017 at 17:46
-
\$\begingroup\$ well one of the arrays is empty.... :-/ \$\endgroup\$– OC_RaizWJan 25, 2017 at 12:37
-
\$\begingroup\$ Hmmm, well that must mean your trying to access something that doesn't exist. You've gotta be very exact when making the path to the specific transition you're looking for. \$\endgroup\$– Sam RFeb 13, 2017 at 4:05