Unity如何使用编辑器脚本删除StateMachineBehaviour

时间:2017-03-14 15:07:06

标签: unity3d

我在使用编辑器脚本删除statemachinebehavi时遇到问题,如下所示:

enter image description here

我使用AnimatorState.AddStateMachineBehaviour添加行为

enter image description here

文档说使用Object.Destroy,我使用这个api,但它出现了:

enter image description here

我想知道如何使用编辑器脚本来实现该功能"删除"

enter image description here

感谢任何想法!!!

1 个答案:

答案 0 :(得分:1)

为了删除状态机行为,您必须获取状态机行为数组,删除您之后的行为,然后重新分配该数组。

这些方面应该做的事情:

using UnityEditor;
using UnityEditor.Animations; 

//how you invoke this is up to you
[MenuItem("CONTEXT/StateMachineBehaviour/Remove Test")]
public static void RemoveBehaviour(MenuCommand command)
{
    Object selection = Selection.activeObject;

    AnimatorState state = selection as AnimatorState;

    if(state != null)
    {
        StateMachineBehaviour behaviour = command.context as StateMachineBehaviour;

        StateMachineBehaviour[] theBehaviours = state.behaviours;

        ArrayUtility.Remove(ref theBehaviours, behaviour);

        Undo.RegisterCompleteObjectUndo(state, "Removed behaviour");

        Undo.DestroyObjectImmediate(behaviour);

        state.behaviours = theBehaviours;
    }                
}

这将使用状态机下拉菜单删除行为,添加撤消和重做支持,可选的奖励。 根据您希望如何处理删除,这种方法会发生变化,但就删除行为而言,这应该是您所追求的。 此外,对于动画师状态机,方法完全相同,您只需将选择对象转换为AnimatorStateMachine。

希望这有帮助。