script | 要实例化的 MonoScript 类。 |
int 返回创建对象的实例 ID,如果某些内容无效则返回 0。
此函数将根据此脚本中定义的类创建 StateMachineBehaviour 实例。
此函数将验证单脚本是否为有效的状态机行为,该类必须是 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()); } }