unity工具相关: NodeCanvas_Example Scenes ( Unity 2018.4.14+ ) 案例笔记

案例来源:

https://nodecanvas.paradoxnotion.com/downloads/

Example Scenes ( Unity 2018.4.14+ )

 

一:BTree Events

 

 

 

Demonstrates how events can be used in the context of Behaviour Trees to react to changes as well as transfer data. In this case float data. In reality, events are best dispatched through code.

演示 BTree内 event的传递数据过程。

关键点:

1.EventRouter的概念

(1)agent的gameObject节点挂接EventRouter组件。

(2)eventRouter的传递过程:task->graphOwner<-graph。 

  task:Condition和Action的抽象。eventRounter在Condition内add remove监听。 在Action内触发派发

  task从graphOwner获取eventRounter。(原因:考虑到可能不是本Graph的eventRounter。用GraphOwner更灵活

  graph内进行真正的event派发

 (3)派发是否针对所有graphs。graph内维持了static graphs,可以获取当前所有运行的graph

(4)EventData的概念,泛型,并且避免装箱 

        void OnCustomEvent(string eventName, IEventData data) {
            if ( eventName.Equals(this.eventName.value, System.StringComparison.OrdinalIgnoreCase) ) {
                if ( data is EventData<T> ) { //avoid boxing if able
                    saveEventValue.value = ( (EventData<T>)data ).value;
                } else if ( data.valueBoxed is T ) { saveEventValue.value = (T)data.valueBoxed; }

                Logger.Log(string.Format("Event Received from ({0}): '{1}'", agent.gameObject.name, eventName), LogTag.EVENT, this);
                YieldReturn(true);
            }
        }

  

 二:IfThenElse with Trigger

(1)是EventRouter类内  1.自定义event事件 2.各种unity 自带函数事件

(2)BTree的驱动UpdateMode模式。如果是Manual,则项目自己UpdateBehaviour驱动。其他模式下,根据插件自带的MonoManager进行mono驱动。

 

三:Imaginary Cat

Conditional的isDynamic:

(1)false情况下,如果Conditional的check通过,且ChildTree为Running,则不再check。直到Running结束,Conditional重新进行check

(2)true的情况下,如果Conditional的check通过,且ChildTree为Running,还是每次进行check。

 

四:Iterator Controller

In this example all 3 cubes are controled with a single "manager" behaviour, with the help of the Iterator Decorator.

(1)Iterator Decorator迭代装饰器的使用。

  程序理解的for循环;

  如果子节点返回running,则中断返回running;

如果resetIndex为false,则要等待rootNode在Reset后才能重新进行迭代。

        protected override Status OnExecute(Component agent, IBlackboard blackboard) {
      
       //1.先处理各种异常情况,如果没有子节点,则返回Optional,无意义。 如果子节点有 list异常,则返回faiure
if ( decoratedConnection == null ) { return Status.Optional; } if ( list == null || list.Count == 0 ) { return Status.Failure; }
       //2.从currentIndex开始。 for ( var i = currentIndex; i < list.Count; i++ ) { current.value = list[i]; storeIndex.value = i; status = decoratedConnection.Execute(agent, blackboard);           //2.处理收到first影响的情况 if ( status == Status.Success && terminationCondition == TerminationConditions.FirstSuccess ) { return Status.Success; } if ( status == Status.Failure && terminationCondition == TerminationConditions.FirstFailure ) { return Status.Failure; }           //2.如果子节点返回running 则返回running if ( status == Status.Running ) { currentIndex = i; return Status.Running; }          //  如果循环结束,下一次是否从头开始,还是不再执行。           if ( currentIndex == list.Count - 1 || currentIndex == maxIteration.value - 1 ) { if ( resetIndex ) { currentIndex = 0; } return status;//最后一个节点决定了我的返回状态 }           //节点执行完毕后reset decoratedConnection.Reset(); currentIndex++; }         //不再执行。默认返回running return Status.Running; } protected override void OnReset() { if ( resetIndex ) { currentIndex = 0; } }

  

(2)BehaviourTree.cs类中关于reset的调用:

如果root节点不是running状态,则reset所有node

        Status Tick(Component agent, IBlackboard blackboard) {
            if ( rootStatus != Status.Running ) { primeNode.Reset(); }
            return rootStatus = primeNode.Execute(agent, blackboard);
        }

(3)Setter装饰器

Set another Agent for the rest of the Tree dynamicaly from this point and on. All nodes under this will be executed for the new agent. You can also use this decorator to revert back to the original graph agent, which is useful to use after another OverrideAgent decorator for example 

设置子Tree的默认agent为指定transform。如果设置为null,则恢复成这个graph原有的agent

 

五:Turret Targeting

六:Variables Data Binding & Implemented Action

Showcase how you can interact with your code by using DataBound blackboard variables and executing Actions implemented in your own scripts. In this case the DataBindExample script.

行为树和BoundSelf对象的引用关系,并且通过反射执行BoundSelf对象的函数和变量的修改

 

posted @ 2022-04-03 21:40  sun_dust_shadow  阅读(194)  评论(0编辑  收藏  举报