[Unity] Unity 插件Behavior Designer行为树使用

这篇具有很好参考价值的文章主要介绍了[Unity] Unity 插件Behavior Designer行为树使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Unity 插件Behavior Designer行为树使用

1 创建行为树

  1. 在Tools -> Behavior Designer -> Editor中打开行为树编辑编辑窗口
  2. 选择一个游戏物体
  3. 在Behavior Designer中右键Add Behavior Tree
    unity 行为树插件,Unity,unity,游戏引擎

2 认识三个基础的组件

2.1 Parallel

组件下方的行为会平行执行
unity 行为树插件,Unity,unity,游戏引擎

分别为下方的三个组件添加了三个输出到Console的Action,可以发现Console中在同一时间做了三个输出

2.2 Sequence

组件下方的行为会按照从左到右的顺序依次执行

unity 行为树插件,Unity,unity,游戏引擎

可以从输出的时间发现,Sequence下方的行为是依次执行的

当所有子节点都返回Success时,它才返回Success

当某个子节点返回Failure时,顺序节点就会立刻返回Failure

2.3 Selector

组件下方的行为会选择执行

unity 行为树插件,Unity,unity,游戏引擎

组件下方的行为会按照选择的次序执行,类似于if else…,一旦前面的执行成功,后面的就不会再执行

如果所有子节点都返回Failure,选择节点才返回Failure

3 使用Behavior Tree中的变量

3.1 添加变量

  1. 在Behavior Designer -> Variables中输入变量名和类型
  2. 点击Add添加变量

unity 行为树插件,Unity,unity,游戏引擎

3.2 使用变量

  1. 创建一个Random Int的行为,将其生成的随机值保存到刚刚创建的RandomNumber中

unity 行为树插件,Unity,unity,游戏引擎

  1. 使用Log Value行为,输出刚刚创建的RandomNumer

    unity 行为树插件,Unity,unity,游戏引擎
    可以发现,最终生成了一个随机数3,并输出了出来
    unity 行为树插件,Unity,unity,游戏引擎

3.3 拓展变量类型

namespace BehaviorTreeVariable
{
    // 书写一个自己的变量类
    public class Student
    {
        public string name;
        public int age;
        public override string ToString()
        {
            return $"name: {name}, age: {age}";
        }
    }
}

namespace BehaviorDesigner.Runtime
{
    // 除了需要书写一个自己的变量类以外,还需要写一个Shared类,只有Shared类的变量才能显示在Behavior Tree的Inspector面板中
    [System.Serializable]
    public class SharedStudent : SharedVariable<Student>
    {
        public static implicit operator SharedStudent(Student stu)
        {
            return new SharedStudent { mValue = stu };
        }
    }
}

在 BehaviorDesigner.Runtime 中写 Shared 类需要遵循以下格式

namespace BehaviorDesigner.Runtime
{
    [System.Serializable]
    public class SharedT : SharedVariable<T>
    {
        public static implicit operator SharedT(T value)
        {
            return new SharedT { mValue = value };
        }
    }
}

这样就可以在变量类型列表中看到我们拓展的 Student 变量类型了

通过代码获取变量:

public SharedVariable GetVariable(string name);              // 获取变量值
public void SetVariable(string name, SharedVariable item);   // 为变量赋值
var bt = GetComponent<BehaviorTree>();

// 访问成员变量
var stu = (SharedStudent)bt.GetVariable("Ousun");
Debug.Log(stu.Value.ToString());

// 修改变量值
stu.Value.name = "Ousun";       // 修改变量值内的成员变量内容
bt.SetVariable("Ousun", stu);   // 将stu变量赋值给Ousun 

// 构建新的成员对象
SharedStudent newSharedStu  = new SharedStudent();
Student newStu = new Student();
newStu.name = "Ousun";
newStu.age = 20;
newSharedStu.SetValue(newStu);

// 赋值成员变量
bt.SetVariable("Ousun", newSharedStu);

3.4 补充:Action与Conditional的区别

  • Action可以理解为执行了一个方法,基本不存在失败的情况
  • Conditional涉及到判断的正确与否

4 自定义Action

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 需要包含额外的头文件
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

public class MyRandomInt : Action   // 继承Action
{
    public SharedInt randomNum;   // Sharedxxx类型的变量即为可以显示在BehaviorDesigner-Inspector中的变量
    // 如果不需要在Inspector中显示,则不用加Shared,直接定义即可

    // 重写OnUpdate()方法,注意OnUpdate()方法有返回值,返回值为TaskStatus类型
    public override TaskStatus OnUpdate()
    {
        randomNum.Value = Random.Range(5, 10);   // 生成随机数,注意使用.Value
        return TaskStatus.Success;               // 返回成功
        // Action类型一般不会返回Failure
    }
}

unity 行为树插件,Unity,unity,游戏引擎

测试:自定义相加的行为,并输出结果

定义行为如下

public class MyAdd: Action
{
    public SharedFloat num1;
    public SharedFloat num2;
    public SharedFloat result;

    public override TaskStatus OnUpdate()
    {
        result.Value = num1.Value + num2.Value;
        return TaskStatus.Success;
    }
}

Behavior Tree如下:

unity 行为树插件,Unity,unity,游戏引擎

输出结果如下:

unity 行为树插件,Unity,unity,游戏引擎

5 自定义Conditional

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 同样需要包含额外的头文件
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

public class MyBiggerThan : Conditional   // 继承Conditional
{
    public SharedInt num1;   // Inspector中的num1
    public SharedInt num2;   // Inspector中的num2

    // 重写OnUpdate()方法,注意OnUpdate()方法有返回值,返回值为TaskStatus类型
    public override TaskStatus OnUpdate()
    {
        if (num1.Value > num2.Value)
            return TaskStatus.Success;
        else
            return TaskStatus.Failure;
    }
}

unity 行为树插件,Unity,unity,游戏引擎

测试:输出比较结果

MyBiggerThan类不变,Behavior Tree如下:

unity 行为树插件,Unity,unity,游戏引擎

输出结果:

unity 行为树插件,Unity,unity,游戏引擎

交换num1和num2的值后的输出结果

unity 行为树插件,Unity,unity,游戏引擎

其他需要override的函数参考:

//This is the agent for whom the action will take place.
public Component agent {get;}
 
//This is the blackboard that you can use to read/write variables manualy if needed.
public IBlackboard blackboard {get;}
 
//This is the time in seconds the action is running.
public float elapsedTime {get;}
 
//Called only the first time the action is executed and before anything else.
//Return null if everything is OK. Return an info string if there is a problem.
virtual protected string OnInit()
 
//Called once when the action is executed.
virtual protected void OnExecute()
 
//Called every frame while the action is running.
virtual protected void OnUpdate()
 
//Called when the action stops for any reason.
//Either because you called EndAction or cause the action was interrupted.
virtual protected void OnStop()
 
//Called when the action is paused comonly when it was still running while the behaviour graph gets paused.
virtual protected void OnPause()
 
//Send an event to the behaviour graph. Use this along with the CheckEvent condition.
protected void SendEvent(string)
 
//Similar to above, but sends a value along with the event.
protected void SendEvent<T>(string, T)
 
//You can use coroutines from within action task as normal.
protected Coroutine StartCoroutine(IEnumerator)
 
//You must call this to end the action. You can call this from wherever you want,
//although typicaly is done in either OnExecute or OnUpdate
public void EndAction(bool)

6 保存/引用行为树资源文件:

6.1 保存行为树资源文件

选中Behavior Designer的右上角Export导出

unity 行为树插件,Unity,unity,游戏引擎

为行为树添加外部资源文件

unity 行为树插件,Unity,unity,游戏引擎

6.2 引用行为树资源文件

在行为树中添加Action -> Behavior Tree Reference

设置其Externel Behaviour为1,并将资源文件拖拽进去即可

unity 行为树插件,Unity,unity,游戏引擎

7 Action使用补充

7.1 Has Received Event:接收事件节点

使用Has Received Event来监听事件,当它检测收到事件时才会返回Success

设置监听:定义一个字符串即可

unity 行为树插件,Unity,unity,游戏引擎

抛出监听事件有以下两种方法:

  1. 调用行为树的SendEvent接口
behaviorTree.SendEvent("LogEvent");   // 其中behaviorTree是行为树的对象
  1. 使用Send Event的Action,设置好需要发送的字符串即可

unity 行为树插件,Unity,unity,游戏引擎

测试时出现了问题,不知道为啥

7.2 Parallel Selector:并行选择节点

如果其中有一个执行成功(返回Success)就会立即返回Success,其他节点返回Failure

当所有的子节点都执行失败时,Parallel Seclector才会返回Failure

7.3 Priority Selector:优先选择节点

Priority Selector会将子节点按照优先级进行排序,选择优先级高的(优先级数字大的)执行

想要设置节点的优先级,需要创建一个脚本重写Task的GetPriority方法,返回一个浮点类变量即可

public class PriorityLog : Action
{
    public SharedFloat priority;
    public SharedString logString;

    public override TaskStatus OnUpdate()
    {
        Debug.Log(logString.Value);
        return TaskStatus.Success;
    }

    public override float GetPriority()
    {
        return priority.Value;
    }
}

7.4 Random Selector:随机选择节点

从子节点中随机选择一个执行,如果有字节点返回Success,则立即返回Suceess,否则继续随机选择执行下一个子节点

只有所有的子节点都返回Failure时,随机选择节点才会返回Failure

7.5 Random Sequence:随机顺序节点

将子节点按照随机的顺序执行,当其中的某个子节点返回Failure时,会立刻返回Failure

7.6 Parallel Complete:并行竞争节点

所有的子节点并行执行,只要有一个子节点先返回了结果,就结束,并返回这个子节点执行的结果

7.7 Selector Evaluator:评估选择节点

从左到右执行子节点,

  • 如果遇到字节点返回Success,即立即结束,返回Success
  • 如果遇到字节点处于Wait的Running状态,会返回前一个子节点重新执行
  • 否则继续执行下一个子节点

8 中断

复合节点有中断的权利,选中某个复合节点,然后点击 Inspector 即可设置它的中断模式,Behavior Tree 的中断是通过运行是创建的 Behavior Manager 控制的

unity 行为树插件,Unity,unity,游戏引擎

8.1 None:无中断

8.2 Self:中断复合节点自己(即中断下方的节点)

unity 行为树插件,Unity,unity,游戏引擎

如图中的行为树,先执行 Wait 5s 的状态,如果在这 5s 内按下了 A 键,则执行 Self 中断,中断 Selector 下方的逻辑,Selector 左边返回 Success,继续执行Sequence逻辑

8.3 Low Priority:中断比自己低优先级的节点(即中断右边的节点)

unity 行为树插件,Unity,unity,游戏引擎

如图中的行为树,Selector 左侧没有返回结果,执行右边的 wait 10s 的操作,此时按下 A 键,立即中断了右方的 Wait 逻辑,输出 Get A Down

8.4 Both:Self 和 Low Priority 节点都中断

unity 行为树插件,Unity,unity,游戏引擎

如图中的行为树,Get A Down 可以同时中断 Selector 下方和右侧的 Wait 操作。需要注意的是,由于按下 A 键可以同时中断右侧和下方的操作,一旦按下 A 键松开后,Selector 下方的 Wait 操作会重新执行。文章来源地址https://www.toymoban.com/news/detail-679543.html

9 行为树事件

9.1 Send Event 和 Has Received Event 节点

  • Target GameObject:可以选择事件发送的游戏物体,可以在 Variable 中添加游戏物体并赋值
  • Event Name:事件名称,需要和接收事件的 Has Received Event 节点中的 Event Name 名称同步
  • Group:由于一个物体可以挂载多个 Behaviour Tree 组件,可以通过 Group 为 Behavior Tree 分组,如果 Send Event 的 Group 为 i,那么这个物体上所有 Group 为 i 的 Behaviour Tree 也会收到事件,反之,其他 Group 的Behavior Tree 不会收到事件
  • Argument:用于发送一些额外的参数,可以与 Has Received Event 节点中的 Stored Value 节点相对应

9.2 通过代码控制行为树事件

9.2.1 注册事件的相应函数
  • public void RegisterEvent<T, U, V>(string name, Action<T, U, V> handler);
  • public void RegisterEvent<T, U>(string name, Action<T, U> handler);
  • public void RegisterEvent(string name, Action handler);
  • public void RegisterEvent(string name, System.Action handler);
9.2.1 注销事件的相应函数
  • public void UnregisterEvent<T, U, V>(string name, Action<T, U, V> handler);
  • public void UnregisterEvent<T, U>(string name, Action<T, U> handler);
  • public void UnregisterEvent(string name, Action handler);
  • public void UnregisterEvent(string name, System.Action handler);

到了这里,关于[Unity] Unity 插件Behavior Designer行为树使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 【Unity 实用工具篇】✨| ReferenceFinder 引用查找插件,提高引擎查找使用效率

    【Unity 实用工具篇】✨| ReferenceFinder 引用查找插件,提高引擎查找使用效率

    前言 ReferenceFinder 是一个比较小众的插件

    2024年02月14日
    浏览(14)
  • 十八、Unity游戏引擎入门

    十八、Unity游戏引擎入门

    1、下载     首先需要下载Unity Hub,下载网址:https://unity.com/cn。     然后在其中下载Unity编辑器并安装,可选择最新版本。     接着需要选择适合的开发环境,例如Android Studio或Xcode,以便进行手机游戏开发。在安装完Unity后,需要根据项目需求下载对应的模块和插件,例

    2024年02月16日
    浏览(50)
  • Unity、UE、Cocos游戏开发引擎的区别

    Unity、Unreal Engine(UE)和Cocos引擎是三个常用的游戏开发引擎,它们在功能和特性上有一些区别。以下是它们之间的主要区别: 编程语言:Unity使用C#作为主要的编程语言,开发者可以使用C#脚本进行游戏逻辑编写。Unreal Engine主要使用C++作为编程语言,但也支持蓝图系统,允许

    2024年02月22日
    浏览(46)
  • Unity vs Godot :哪个游戏引擎更适合你?

    Unity vs Godot :哪个游戏引擎更适合你?

    游戏引擎的选择对开发过程和最终产品质量有着重大影响。近年来,Godot和Unity这两款引擎受到广泛关注。本文将从多个维度对两者进行比较,以期为开发者提供正确的选择建议。 Godot和Unity都有各自的优势,没有绝对的好坏之分。Godot开源免费,上手简单,更适合2D和小型游戏

    2024年01月23日
    浏览(56)
  • 30分钟了解所有引擎组件,132个Unity 游戏引擎组件速通!【收藏 == 学会】

    30分钟了解所有引擎组件,132个Unity 游戏引擎组件速通!【收藏 == 学会】

    🎬 博客主页:https://xiaoy.blog.csdn.net 🎥 本文由 呆呆敲代码的小Y 原创,首发于 CSDN 🙉 🎄 学习专栏推荐:Unity系统学习专栏 🌲 游戏制作专栏推荐:游戏制作 🌲Unity实战100例专栏推荐:Unity 实战100例 教程 🏅 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正! 📆 未来很长

    2024年02月11日
    浏览(66)
  • Unity Physics2D 2d物理引擎游戏 笔记

    Unity Physics2D 2d物理引擎游戏 笔记

    2d 材质 里面可以设置 摩擦力 和 弹力 Simulated:是否在当前的物理环境中模拟,取消勾选该框类似于Disable Rigidbody,但使用这个参数更加高效,因为Disable会销毁内部产生的GameObject,而取消勾选Simulated只是禁用。 Kinematic 动力学刚体 动力学刚体不受重力和力的影响,而受用户的

    2023年04月24日
    浏览(43)
  • 新版UNITY游戏(IL2CPP类型)使用内嵌型机器翻译插件XUnity.AutoTranslator的食用方法

    #新版UNITY游戏(IL2CPP类型)使用内嵌型机器翻译插件XUnity.AutoTranslator的食用方法# 文档更新时间:2022/4/3 20:02:50 网络具备访问github.com的能力 能够进行网页翻译的浏览器(用来查看官方的说明文档),以及耐心 网络能够访问机器翻译服务(谷歌等),如果使用百度需要注册百

    2024年02月09日
    浏览(32)
  • GODOT游戏引擎简介,包含与unity性能对比测试,以及选型建议

    GODOT游戏引擎简介,包含与unity性能对比测试,以及选型建议

    GODOT,是一个免费开源的3D引擎。本文以unity作对比,简述两者区别和选型建议。由于是很久以前写的ppt,技术原因视频和部分章节丢失了。建议当做业务参考。 GODOT目前为止遇到3个比较重大的机遇,第一个是oprea的合作奖,第二个是用支持c#换来的微软的投资,第三个是虚幻

    2024年02月14日
    浏览(112)
  • Unity和UE4两大游戏引擎,你该如何选择?

    Unity和UE4两大游戏引擎,你该如何选择?

    目录 游戏引擎 2 —— 难易区别 编程语言 3 —— 游戏产品 UE4制作的游戏产品  Unity制作的游戏产品  产品类型 5 —— 资源商店 6 —— 人才需求 平均薪资 总结      Unity和UE4都是游戏引擎,所谓游戏引擎就是集成了复杂功能的游戏开发软件,他们帮我们实现了复杂的底层逻

    2023年04月08日
    浏览(50)
  • Unity 开发人员转CGE(castle Game engine)城堡游戏引擎指导手册

    Unity 开发人员转CGE(castle Game engine)城堡游戏引擎指导手册

    一、简介 2. Unity相当于什么GameObject? 3. 如何设计一个由多种资产、生物等组成的关卡? 4. 在哪里放置特定角色的代码(例如生物、物品)?Unity 中“向 GameObject 添加 MonoBehaviour”相当于什么? 5.Unity子目录相当于什么Assets? 6. 支持哪些模型格式? 7. 支持FBX模型格式吗? 8.

    2024年02月07日
    浏览(17)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包