Version: 2022.2

AnimatorController.CreateStateMachineBehaviour

切换到手册
public static int CreateStateMachineBehaviour (MonoScript script);

参数

script 要实例化的 MonoScript 类。

返回

int 返回所创建对象的实例 ID,如果有无效内容,则返回 0。

描述

此函数将根据此脚本中的类定义创建一个 StateMachineBehaviour 实例。

此函数将验证 Monoscript 是有效的状态机行为,此类必须是 StateMachineBehaviour 的子类并且不能是泛型类。 另请参阅:StateMachineBehaviour

using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;

public class AddSMB { public void DoAddStateMachineBehaviour(UnityEditor.Animations.AnimatorState state, MonoScript monoScript) { if (state == null) return;

int instanceID = AnimatorController.CreateStateMachineBehaviour(monoScript); if (instanceID == 0) { Debug.LogError("Could not create state machine behaviour " + monoScript.name); return; }

state.AddStateMachineBehaviour(monoScript.GetClass());

var obj = EditorUtility.InstanceIDToObject(instanceID); if (obj == null) Debug.LogError("No object could be found with instance id: " + instanceID); else AssetDatabase.AddObjectToAsset(obj, state.ToString()); } }