-
Is there any way i can find AnimatorController parameter usage |
Beta Was this translation helpful? Give feedback.
Answered by
yasirkula
Aug 20, 2024
Replies: 1 comment 5 replies
-
i created this with the help of ChatGPT but the names are not correct also they just ping to asset not the specific transitions or blend tree void ShowAnimatorParameterReferences()
{
Animator[] animators = Selection.GetFiltered<Animator>(SelectionMode.Assets);
if (animators != null && animators.Length > 0)
{
var animator = animators[0];
if (animator)
{
var controller = animator.runtimeAnimatorController as AnimatorController;
if (controller != null)
{
animatorControllerParameters = controller.parameters.Select(p => p.name).ToArray();
if (animatorControllerParameters.Length > 0)
{
animatorParameterIndex = EditorGUILayout.Popup("Parameter", animatorParameterIndex, animatorControllerParameters);
if (GUILayout.Button("Find Usage"))
{
FindParameterUsage(controller, animatorControllerParameters[animatorParameterIndex]);
}
}
}
}
}
}
private void FindParameterUsage(AnimatorController controller, string paramName)
{
bool found = false;
foreach (var layer in controller.layers)
{
var stateMachine = layer.stateMachine;
found = FindStatesRecursive(paramName, found, layer, stateMachine, stateMachine.name);
}
if (!found)
{
Debug.Log($"Parameter '{paramName}' not found in any transitions or blend trees.");
}
}
private bool FindStatesRecursive(string paramName, bool found, AnimatorControllerLayer layer, AnimatorStateMachine stateMachine, string path)
{
foreach (var state in stateMachine.states)
{
var currentPath = $"{path}/{state.state.name}";
found = FindInStates(paramName, found, layer, state, currentPath);
}
foreach (var childStateMachine in stateMachine.stateMachines)
{
var currentPath = $"{path}/{childStateMachine.stateMachine.name}";
found = FindStatesRecursive(paramName, found, layer, childStateMachine.stateMachine, currentPath);
}
return found;
}
private bool FindInStates(string paramName, bool found, AnimatorControllerLayer layer, ChildAnimatorState state, string path)
{
foreach (var transition in state.state.transitions)
{
foreach (var condition in transition.conditions)
{
if (condition.parameter == paramName)
{
found = true;
Debug.Log($"Parameter '{paramName}' used in transition in path '{path}' in layer '{layer.name}'.", transition);
}
}
}
if (state.state.motion is BlendTree blendTree)
{
if (IsParameterUsedInBlendTree(blendTree, paramName))
{
found = true;
Debug.Log($"Parameter '{paramName}' used in BlendTree '{blendTree.name}' in path '{path}' in layer '{layer.name}'.", blendTree);
}
}
return found;
}
private bool IsParameterUsedInBlendTree(BlendTree blendTree, string paramName)
{
if (blendTree.blendParameter == paramName || blendTree.blendParameterY == paramName)
{
return true;
}
foreach (var child in blendTree.children)
{
if (child.motion is BlendTree childBlendTree)
{
if (IsParameterUsedInBlendTree(childBlendTree, paramName))
{
return true;
}
}
}
return false;
} |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you try deleting the parameter, Unity tells you where it's used. If it isn't used, the parameter is deleted without a dialog.