操作方式:project中选择要操作的animator controller 之后去顶部菜单栏找到你创建的[MenuItem("Tools/MyFunction")] 点击运行方法
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
public class AnimatorControllerUtility
{
[MenuItem("Tools/Create Transitions to Exit")]
public static void CreateTransitionsToExit()
{
// Get the selected Animator Controller
AnimatorController animatorController = Selection.activeObject as AnimatorController;
if (animatorController == null)
{
Debug.LogError("Please select an Animator Controller.");
return;
}
// Loop through all layers in the Animator Controller
foreach (var layer in animatorController.layers)
{
var stateMachine = layer.stateMachine;
// Loop through all states in the layer
foreach (var state in stateMachine.states)
{
AnimatorState animatorState = state.state;
// Check if there is already a transition to exit from this state
bool hasExitTransition = false;
foreach (var transition in animatorState.transitions)
{
if (transition.destinationState == null)
{
hasExitTransition = true;
break;
}
}
if (!hasExitTransition)
{
AnimatorStateTransition transitionToExit = animatorState.AddExitTransition(); // Adding transition to exit implicitly
transitionToExit.hasExitTime = true;
transitionToExit.exitTime = 1f; // Ensure it triggers when the state finishes
transitionToExit.duration = 0f; // Optional: Set duration of the transition
}
}
}
Debug.Log("Transitions to Exit created for all states.");
}
}