Laya 踩坑日记 ---A* 导航寻路

要做寻路,然后看了看laya 官方的例子,感觉看的一脸懵逼,早了半天的api 也没找到在哪有寻路的,最后一看代码,原来是用的github上的A星方案  https://github.com/bgrins/javascript-astar

这就尴尬了,,然后研究了一下,果然还是要unity支持,导出烘焙过的地图信息,在网上找了一些方案,然后自己也做了一下修改和调整,现在给大家发出来,(当做参考用 。。。)

需要unity 插件 A*   (AstarPathfindingProject  )

然后配置好地图,然后烘焙

 

然后 通过AstartExpLayaTool  /exp json   导出地图的json信息 (json信息 已自动复制到 剪切板中了)

在通过laya 去调用

 

Laya 这边 将json 信息给存起来然后调用

通过github 把 astar.js 给下载下来,然后放到项目的libs下,编写  .d.ts  辅助文件

 

复制代码
declare class astar {
      /**
         * Perform an A* Search on a graph given a start and end node.
         * @param {Graph} graph
         * @param {GridNode} start
         * @param {GridNode} end
         * @param {Object} [options]
         * @param {bool} [options.closest] Specifies whether to return the
                     path to the closest node if the target is unreachable.
        * @param {Function} [options.heuristic] Heuristic function (see
        *          astar.heuristics).
        */
    static search(graph, start, end, options?);
    cleanNode(node);
}

declare class Graph {
    /**
     * A graph memory structure
     * @param {Array} gridIn 2D array of input weights
     * @param {Object} [options]
     * @param {bool} [options.diagonal] Specifies whether diagonal moves are allowed
     */
    constructor(gridIn, options?)
    grid: any;
   
}

declare class GridNode {
    x;
    y;
    weight;
    constructor(x?, y?, weight?)
}
复制代码

然后编写自己寻路代码就行了

 

 

备注:此方案当前性能偏低,将坐标对应矩阵部分有问题,但作为一个参考还是可以的

laya地址  https://gitee.com/eryuefeng/laya-astar-pathfinding

 

posted @   nuolo  阅读(686)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示