unity⾏为树教程_教程:⾏为2
unity⾏为树教程
Hello everybody! My name is Emil and I will be your guide on this tour of Behave 2 for Unity.
Having previously worked at Unity Technologies from April 2009 to July 2013, I am now a
freelance game AI developer under my company – and nickname – “AngryAnt”.
⼤家好! 我的名字叫Emil,我将作为您的⾏为指南2 Unity的向导。 我曾于2009年4⽉⾄2013年7⽉在Unity Technologies⼯作,现在我是公司的⾃由职业游戏AI开发⼈员,绰号为“ AngryAnt”。
背景 (Background)
科技主题绘画The Behave project, along with Path (now open-source MIT), were among the first projects I did in Unity after picking it up in the beginning of 2008. In an all too familiar story, I created the tools to replace tho I had ud at my previous job, but ended up focusing more on the tools than my game project.
Behave项⽬以及Path(现在是MIT的开源项⽬)是我在2008年初开始使⽤Unity之后所做的第⼀个项⽬。在⼀个⾮常熟悉的故事中,我创建了替换我拥有的⼯具的⼯具。在我以前的⼯作中使⽤过,但最终⽐我的游戏项⽬更多地关注⼯具。一本倒
This was Behave 0.3b. 1.0 was relead a couple of months later and 1.2 went live in 2010 as one of the launch packages of the Ast Store.
这是⾏为0.3b。 ⼏个⽉后,1.0发布了,⽽1.2作为Ast Store的启动软件包之⼀于2010年上线。
When at Unity, my schedule was pretty packed, so 2.0 was quite a while under way. Large refactors, support for multiple engines and platforms plus feature creep did not help much either. But here we are now on 2.3.2 – fortunately updates since the relea of 2.0 in August 13 did not take the time that 1.4→2.0 did.
在Unity时,我的⽇程安排⾮常紧凑,因此2.0进⾏了相当长的时间。 ⼤型重构,对多个引擎和平台的⽀持以及功能爬升也⽆济于事。 但是现在我们在2.3.2上了-幸运的是,⾃8⽉13⽇发布2.0以来,更新所花的时间没有1.4→2.0所花的时间。
Overview
总览
So with the history lesson out of the way, what is Behave, really? In short, it is an AI behaviour logic system. It allows you to visually design behaviour trees, link them directly to your own code through a highly efficient compiler and finally debug the trees, running in the editor or on your target device.
照相机英文因此,与历史课的出路,什么是循规蹈矩,真的吗? 简⽽⾔之,它是⼀个AI⾏为逻辑系统。 它允许您直观地设计⾏为树,通过⾼效的编译器将它们直接链接到您⾃⼰的代码,最后调试这些树,这些树可以在编辑器中或在⽬标设备上运⾏。
One of the guiding principles behind Behave is to as much as possible avoid systemic requirements. That is designs which might chain your runtime integration into a certain way of operating. The result is a very lean and efficient runtime, with the integration possibilities more or less just limited to your imagination and practical needs.
⾏为背后的指导原则之⼀是尽可能避免系统性要求。 这些设计可能会将您的运⾏时集成链接到某种操作⽅式。 结果是⾮常精简和⾼效的运⾏时,集成的可能性或多或少只限于您的想象⼒和实际需求。
⾏为树 (Behaviour trees)
Behaviour trees you say? Yes I do. A widely standardid method of describing behaviour logic, first ud on scale in Halo, behaviour trees t themlves apart from methods like state machines in that they scale much better, are easy to read be made responsive.
你说的⾏为树? 是的,我愿意。 ⾏为树是⼀种⼴泛标准化的描述⾏为逻辑的⽅法,⾸先在Halo中按⽐例使⽤,⾏为树将其与状态机之类的⽅法区分开来,因为它们的可伸缩性更好,并且易于响应。
While state machines are in the business of lecting states within which actions are performed, behaviour trees build state implicitly from their structure and focus squarely on lecting actions to perform.
当状态机负责选择要在其中执⾏动作的状态时,⾏为树会从其结构隐式构建状态,并专注于选择要执⾏的动作。
This means that while state machines allow you to t up states with any number of transitions (at scale often ending up in a hard to maintain spider-web of transitions), behaviour trees have a strict t of rules for connectivity and evaluation.
这意味着,尽管状态机允许您设置具有任意数量的转换的状态(⼤规模地往往以难以维持的转换蜘蛛⽹结尾),但是⾏为树具有⼀套严格的连通性和评估规则。
他们的规则 (Them rules)
A behaviour tree is basically an upside-down tree structure – evaluation starting from the root at the top, filtering through a number of interconnected control nodes and ending in leaf nodes – actions. Actions are where you interface game logic with behaviour logic, hooking up nsors and motors.
⾏为树基本上是⼀种上下颠倒的树结构-动作是从顶部的根开始进⾏评估,通过多个相互连接的控制节点进⾏过滤并在叶节点处结束。 动作是您在游戏逻辑和⾏为逻辑之间进⾏交互,连接传感器和马达的地⽅。
The responsiveness of behaviour trees stems from the fact that they are most often evaluated continuously, at some frame rate. Each evaluation start at the top and given the rules for the different control nodes, the flow is directed until an action node is hit.
⾏为树的响应性源于这样的事实,即最经常以某种帧速率对它们进⾏连续评估。 每次评估都从顶部开始,并为不同的控制节点指定了规则,流程将⼀直进⾏到操作节点被点击为⽌。
Each node will then, rather than block on execution, return a signal back to its parent node. The parent then interprets, reacts and returns its own signal back up the chain until the root is reached again.
然后,每个节点将向其⽗节点返回信号,⽽不是在执⾏时阻塞。 然后,⽗级解释,做出React并在链中返回⾃⼰的信号,直到再次到达根为⽌。
This signal can be one of three: Success, Failure or Running. Success and Failure obviously meaning that the node succeeded or failed in its task and Running meaning that the node has not yet reached the conclusion of its task and requests to get re-pinged on the next tree evaluation.
该信号可以是以下三种之⼀:成功,失败或正在运⾏。 成功和失败显然意味着该节点在其任务中成功或失败,⽽正在运⾏则意味着该节点尚未完成其任务的结束,并要求在下⼀次树评估中重新进⾏ping操作。
Example actions could be HasTarget, which would return Success if the agent executing the tree has a target and otherwi Failure or GoToTarget, which would return Running while on its way to the target and then Success when reached or Failure when determined to be unreachable.
⽰例动作可以是HasTarget,如果执⾏树的代理具有⽬标,则将返回Success;否则,将显⽰Failure或GoToTarget,即在到达⽬标的途中将返回Running,然后在到达⽬标时返回Success,或者在确定⽆法到达时返回Failure。
表现整合 (Behave integration)
So while the graphical editor lets you easily connect together the control nodes and actions, you of cour need to hook this up to your AI agents at some point.
因此,尽管图形编辑器使您可以轻松地将这些控制节点和动作连接在⼀起,但您当然需要在某些时候将其连接到AI代理。
This is achieved via the one-click compilation of your Behave library (the ast containing your trees), which for the Unity target compiler generates a asmbly. As it is output in your asts folder, Unity will automatically compile it in with the rest of your code.
这是通过Behave库(包含树的资产)的⼀键式编译来实现的,该库为Unity⽬标编译器⽣成了⼀个程序集。 当它在您的资产⽂件夹中输出时,Unity会⾃动将其与其余代码⼀起编译。
What this means is that once you hit compile, you will be able to access generated class from your code, reprenting your behaviour trees at runtime.蒸馒头步骤
这意味着⼀旦进⾏编译,您将能够从代码中访问⽣成的类,这些类代表运⾏时的⾏为树。
金属探测仪原理12345678910public class BLMyLibrary
{
public enum TreeType { Path_To_TreeName, …, Unknown };
public enum ContextType { ContextName, …, Unknown };洗碗的拼音
public enum PriorityType { PriorityName, …, Unknown };
public enum ActionType { ActionName, …, Unknown };
public static Tree InstantiateTree (TreeType treeType, IAgent agent); public static TreeType Type (Tree tree);
}
12345678910公共课程BLMyLibrary
{
公共枚举TreeType {Path_To_TreeName,…,未知};
公共枚举ContextType {ContextName,…,未知};
公共枚举PriorityType {PriorityName,…,未知};
公共枚举ActionType {ActionName,…,Unknown};
公共静态树InstantiateTree(TreeType treeType,IAgent代理);公共静态TreeType类型(树);
}开学了作文
hosted with ❤ by 托管于❤的
The central method of the generated library class “BL[AstName]” is the InstantiateTree method. This takes as parameter first the tree type you wish instantiated (via an enum generated from the tree names in the editor) and cond the agent
you wish to integrate the tree with. This is the class which will need to implement the action handlers described earlier.
⽣成的库类“ BL [AstName]”的中⼼⽅法是InstantiateTree⽅法。 这⾸先将要实例化的树类型(通过在编辑器中从树名⽣成的枚举)作为参数,然后将要与之集成树的代理作为参数。 该类将需要实现前⾯描述的动作处理程序。
灵活性 (Flexibility)
Out of the box Behave offers two ways of implementing action handlers. The default is you derive from the IAgent interface. In this ca Behave will reflect your class for action handlers on instantiation, much like the Unity messaging system.
开箱即⽤的⾏为提供了两种实现动作处理程序的⽅式。 默认值为您从IAgent界⾯派⽣。 在这种情况下,⾏为会像实例化Unity消息传递系统⼀样,将您的类反映给实例的动作处理程序。
The cond way of implementing action handlers is to define an agent blueprint in your library. At runtime, this results in an abstract agent class being defined, with predefined virtual handlers for all actions ud by the trees supported by that blueprint. This method is less flexible, but removes the overhead of reflection on tree instantiation and gives you auto-complete on action handler methods in your favourite code editor.
实现动作处理程序的第⼆种⽅法是在您的库中定义代理程序蓝图。 在运⾏时,这导致定义了⼀个抽象代理类,并为该蓝图⽀持的树所使⽤的所有操作定义了预定义的虚拟处理程序。 此⽅法不太灵活,但是消除了树实例化时的反射开销,并使您可以在⾃⼰喜欢的代码编辑器中⾃动完成操作处理程序⽅法。
With handlers defined, you then simply call the Tick method on the tree instance at a frame-rate or in
respon to some game event and the tree will in turn call one or more of your action handlers, depending on its design.
定义处理程序后,您只需以帧速率或响应某些游戏事件在树实例上调⽤Tick⽅法,然后树将根据其设计依次调⽤⼀个或多个动作处理程序。
For core character behaviour logic, I usually create a coroutine named AIUpdate or turn Start into a coroutine, containing a loop which ticks the tree and then yields WaitForSeconds one divided by the frequency property of the tree. This property rves no other purpo at runtime than to communicate an intend from the designer to the programmer.活着书
对于核⼼⾓⾊⾏为逻辑,我通常创建⼀个名为AIUpdate的协程,或者将Start变成协程,其中包含⼀个循环,该循环对树进⾏滴答,然后⽣成WaitForSeconds除以树的频率属性)。 此属性在运⾏时没有其他⽤途,只能将设计⼈员的意图传达给程序员。
So as you can already e at this point, Behave does indeed follow the design goal of low complexity, leaving design decisions in the integration layer completely up to you.
因此,如您在此时已经看到的那样,Behave确实遵循了低复杂度的设计⽬标,完全由您⾃⼰决定集成层中的设计决策。
The Behave runtime has much more runtime API and integration flexibility, but that is unfortunately a bit much to cover in this overview.
Behave运⾏时具有更多的运⾏时API和集成灵活性,但是不幸的是,在本概述中要涉及到很多。
紧急⾏动 (EOF)
I hope you found this introduction uful and that you will consider using Behave for your next AI project. I would recommend you check out the following sources for more information on behaviour trees:
我希望您对本介绍很有帮助,并希望将Behave⽤于下⼀个AI项⽬。 我建议您查看以下来源,以获取有关⾏为树的更多信息:
And of-cour more information on Behave canbe found at:当然,有关⾏为的更多信息可以在以下位置找到:
Have fun!
玩得开⼼!
Emil “AngryAnt” Johann
埃⽶尔·“愤怒的蚂蚁”约翰森
unity⾏为树教程