unity行为树教程_教程:行为2

unity行为树教程_教程:行为2_第1张图片

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 those I had used at my previous job, but ended up focusing more on the tools than my game project.

Behave项目以及Path(现在是MIT的开源项目)是我在2008年初开始使用Unity之后所做的第一个项目。在一个非常熟悉的故事中,我创建了替换我拥有的工具的工具。在我以前的工作中使用过,但最终比我的游戏项目更多地关注工具。

unity行为树教程_教程:行为2_第2张图片

This was Behave 0.3b. 1.0 was released a couple of months later and 1.2  went live in 2010 as one of the launch packages of the Asset Store.

这是行为0.3b。 几个月后,1.0发布了,而1.2作为Asset 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 release 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 standardised method of describing behaviour logic, first used on scale in Halo, behaviour trees set themselves apart from methods like state machines in that they scale much better, are easy to read be made responsive.

你说的行为树? 是的,我愿意。 行为树是一种广泛标准化的描述行为逻辑的方法,首先在Halo中按比例使用,行为树将其与状态机之类的方法区分开来,因为它们的可伸缩性更好,并且易于响应。

unity行为树教程_教程:行为2_第3张图片

While state machines are in the business of selecting states within which actions are performed, behaviour trees build state implicitly from their structure and focus squarely on selecting actions to perform.

当状态机负责选择要在其中执行动作的状态时,行为树会从其结构隐式构建状态,并专注于选择要执行的动作。

This means that while state machines allow you to set 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 set of rules for connectivity and evaluation.

这意味着,尽管状态机允许您设置具有任意数量的转换的状态(大规模地往往以难以维持的转换蜘蛛网结尾),但是行为树具有一套严格的连通性和评估规则。

他们的规则 (Them rules)

unity行为树教程_教程:行为2_第4张图片

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 sensors 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 otherwise 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 these control nodes and actions, you of course 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 asset containing your trees), which for the Unity target compiler generates a .net assembly. As it is output in your assets folder, Unity will automatically compile it in with the rest of your code.

这是通过Behave库(包含树的资产)的一键式编译来实现的,该库为Unity目标编译器生成了一个.net程序集。 当它在您的资产文件夹中输出时,Unity会自动将其与其余代码一起编译。

What this means is that once you hit compile, you will be able to access generated classes from your code, representing your behaviour trees at runtime.

这意味着一旦进行编译,您将能够从代码中访问生成的类,这些类代表运行时的行为树。

12345678910
public 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类型(树);
}

view rawBLMyLibrary.cs hosted with ❤ by GitHub

查看 GitHub托管于❤的原始 BLMyLibrary.cs

The central method of the generated library class “BL[AssetName]” is the InstantiateTreemethod. This takes as parameter first the tree type you wish instantiated (via an enum generated from the tree names in the editor) and second the agent you wish to integrate the tree with. This is the class which will need to implement the action handlers described earlier.

生成的库类“ BL [AssetName]”的中心方法是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 case Behave will reflect your class for action handlers on instantiation, much like the Unity messaging system.

开箱即用的行为提供了两种实现动作处理程序的方式。 默认值为您从IAgent界面派生。 在这种情况下,行为会像实例化Unity消息传递系统一样,将您的类反映给实例的动作处理程序。

The second 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 used 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 response 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 serves no other purpose at runtime than to communicate an intend from the designer to the programmer.

对于核心角色行为逻辑,我通常创建一个名为AIUpdate的协程,或者将Start变成协程,其中包含一个循环,该循环对树进行滴答,然后生成WaitForSeconds除以树的频率属性)。 此属性在运行时没有其他用途,只能将设计人员的意图传达给程序员。

So as you can already see 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)

unity行为树教程_教程:行为2_第5张图片

I hope you found this introduction useful 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项目。 我建议您查看以下来源,以获取有关行为树的更多信息:

  • Behaviour trees on Wikipedia

    维基百科上的行为树

  • AIgameDev.com

    AIgameDev.com

And of-course more information on Behave canbe found at:

当然,有关行为的更多信息可以在以下位置找到:

  • AngryAnt.com/Behave

    AngryAnt.com/Behave

  • community.AngryAnt.com

    community.AngryAnt.com

Have fun!

玩得开心!

Emil “AngryAnt” Johansen

埃米尔·“愤怒的蚂蚁”约翰森

翻译自: https://blogs.unity3d.com/2014/04/28/tutorial-behave-2-for-unity/

unity行为树教程

你可能感兴趣的:(游戏,python,java,大数据,c++)