/// <summary> /// 开始执行整个动作树。 /// </summary> public void Start() { CurrentAction = _head.GetChild(ActionState.Default); if (CurrentAction != null && CurrentAction.Action != null) { // 立即执行第一个动作。 CurrentAction.Action(); } CurrentState = ActionTreeState.Running; }
/// <summary> /// 由上层的MonoBehaviour调用。 /// </summary> public void Update() { if (CurrentState != ActionTreeState.Running) { return; } if (CurrentAction == null) { // 如果当前没有执行Action,则直接结束所有的动作。 CompleteActionTree(); return; } if (CurrentAction.Checker != null) { // 存在checker,则检查checker的结果。 CurrentAction.Checker(CurrentAction.State); if (CurrentAction.State.Result != ActionState.Continue) { // 不是Contine,则尝试开始下一个动作。 var next = CurrentAction.GetChild(CurrentAction.State.Result); CurrentAction = next; if (CurrentAction != null && CurrentAction.Action != null) { CurrentAction.Action(); } } } else { // 不存在checker,则尝试查找默认的动作。 var next = CurrentAction.GetChild(ActionState.Default); CurrentAction = next; if (CurrentAction != null && CurrentAction.Action != null) { CurrentAction.Action(); } } }