Unity 使用 腾讯Behaviac (三)

这篇博客详细介绍了如何在Unity游戏引擎中利用behaviac库创建和管理行为树,用于实现AI代理的智能行为控制。作者展示了从初始化行为树到加载、执行和更新行为树的关键步骤,包括错误处理和行为树的执行条件检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///<<< BEGIN WRITING YOUR CODE FILE_INIT
///<<< END WRITING YOUR CODE

public class TestAgent: behaviac.Agent
{
	public string bTName = "BT_Tree";

	private behaviac.EBTStatus _status = behaviac.EBTStatus.BT_RUNNING;

	private void Awake()
    {
        initBehaviac();
        initAgent();
    }

    private void Update()
    {
        if (isExcuteTree() == false) return;
        behaviac.Workspace.Instance.DebugUpdate();
        _status = this.btexec();
    }

    private void initBehaviac()
    {
    	// 行为树xml文件路径,我放在了StreamingAssets文件夹下
        behaviac.Workspace.Instance.FilePath = Application.streamingAssetsPath;
        behaviac.Workspace.Instance.FileFormat = behaviac.Workspace.EFileFormat.EFF_xml;

        RegisterInstanceName<TestAgent>("TestAgent");
    }

    private bool initAgent()
    {
        bool bRet = this.btload(bTName );
        if (!bRet)
            UnityEngine.Debug.LogError("Behavior tree data load failed! " + bTName );
        else
            this.btsetcurrent(bTName );

        return bRet;
    }

	// 是否可以执行行为树
	private void isExcuteTree()
	{
	 	return true;
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值