Unity NodeCanvas 可视化行为编辑框架(二)在Lua中使用NodeCanvas框架

本文详细介绍了如何在Unity游戏项目中结合Lua代码有效地运用NodeCanvas行为树框架。主要内容包括:BTConfig.lua脚本的作用,BehaviourTree.lua作为主干脚本的管理逻辑,自定义节点的创建,节点状态及其执行逻辑的说明,并提供了相关源码和资源链接供参考。
摘要由CSDN通过智能技术生成

**

本篇旨在讲解如何在游戏项目中配合Lua代码正常使用NodeCanvans行为树框架

**
tips:本篇篇幅过长,主要是讲解NodeCanvas主要代码及接口

首先介绍几个主要的Lua脚本

一、BTConfig.lua
这个脚本主要用于存放全局的节点,包括NodeCanvas自带的组合节点、连接节点、动作节点以及自定义节点,还用于存放全局的节点状态枚举。
所有自定义的节点都要在这里申明。

local PathToClassMap = {
   
    --组合节点
    ["NodeCanvas.BehaviourTrees.Selector"] = require "Game/LuaBT/BT/Core/Nodes/Composites/Selector",
    ["NodeCanvas.BehaviourTrees.Sequencer"] = require "Game/LuaBT/BT/Core/Nodes/Composites/Sequencer",
    ["NodeCanvas.BehaviourTrees.Parallel"] = require "Game/LuaBT/BT/Core/Nodes/Composites/Parallel",

    --连接节点
    ["NodeCanvas.BehaviourTrees.BTConnection"] = require "Game/LuaBT/BT/Core/BTConnection",

    --动作节点
    ["NodeCanvas.BehaviourTrees.ActionNode"] = require "Game/LuaBT/BT/Core/Nodes/Leafs/ActionNode",
    ["NodeCanvas.BehaviourTrees.ConditionNode"] = require "Game/LuaBT/BT/Core/Nodes/Leafs/ConditionNode",
    ["NodeCanvas.Framework.ActionList"] = require "Game/LuaBT/BT/Core/Tasks/ActionList",

    --自定义节点
    ["Scenario.Wait"] = require "Game/LuaBT/Custom/Actions/Wait",--延时
}

function BTGetClass(path)
    local realClass = PathToClassMap[path]
    if not realClass then
        print(string.format("%s对应的节点不存在", path))
    end
    return realClass
end

BTStatus = {
   
    Failure     = 0,
    Success     = 1, 
    Running     = 2, 
    Resting     = 3, 
    Error       = 4, 
    Optional    = 5
}

二、BehaviourTree.lua
这个脚本是NodeCanvas在Lua应用的主干脚本,用于管理整个行为树的行为逻辑,包括每个节点的执行调用、挂起、停止等等,还有一些必须的接口如加载行为树,加载子行为树等操作。

local BehaviourTree = class("BehaviourTree")
function BehaviourTree:ctor()
    self.id = 0
    self.version = false
    self.type = false
    self.name = "BehaviourTree"
    self.primeNode = false
    self.nodes =  {
   }
    self.nodesIndex = {
   }
    self.subTrees = {
   }
    self.debugList = {
   }
    self.rootStatus = BTStatus.Resting
    self.agent = {
   id = 1001}
    self.agent.isBTDebug = false
    self.blackboard = false
    self.isRunning = false
    self.isPaused = false
    self.isRepeat = false
    self.tickCount = 0
    self.time = 0
end
-- 开始执行行为树
function BehaviourTree:start()
    self.isRunning = true
    self.rootStatus = self.primeNode.status
end
-- 行为树的每帧更新
The complete Visual Behaviour Authoring framework for Unity, empowering you to create advanced AI Behaviours and Logic, including three separate, fully featured, yet seamlessly interchangeable modules for you to choose and easily add in your game: • Behaviour Trees • Hierarchical State Machines • Dialogue Trees NodeCanvas is a production ready framework used by many awesome games including Kim, Pamela, Hurtworld, Shroud of the Avatar, Kingdom and Ghost of a Tale. [Games Showcase] Feature Highlights ------------------------------------- • Choose the right tool for the task at hand. • Design reactive Behaviour Trees and Hierarchical State Machines within an intuitive, designer friendly visual node editor: (Full Undo/Redo, Zoom In/Out, Multi-selection, Duplication, Copy/Pasting, JSON Import/Export, Groups, Comments and more) • Use local & global variables of any type, visually or in code, for creating reusable and actor-oriented, parametric behaviours, optionally saving and loading those variables between gaming sessions. • Data Bind variables with any component property of any type directly. • Sync variables automatically over the network using UNET, for creating multiplayer games. • Visually Debug behaviours with realtime, colorful and informative runtime debugging. • Live Edit everything while in play mode to perfectly understand your design goals and how to achieve them. • Utilize any existing code directly with advanced and extremely fast Reflection Tasks, automatically integrating Unity's and 3rd Party asset APIs. • Work with Lists/Arrays, Enums, Interfaces and pretty much ANY Variable Type you need out-of-the-box. • React to world changes and transfer data using the built-in Event System. • Reuse and Bind made behaviours among any number of different agents. • Organize your designs using Behaviour Sub-Trees and Sub-State Machines. • Extend NodeCanvas Framework to create your own Actions, Conditions, Nodes or even completely new modules with the e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值