资源简介
Unity3d利用A*算法实现寻路模拟
博客地址:blog.liujunliang.com.cn
代码片段和文件信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AStarAlgorithm
{
private const int mGridWidth = 20;
private const int mGridHeight = 10;
//使用二维数组存储点网格
public AStarPoint[] mPointGrid = new AStarPoint[mGridWidthmGridHeight];
//存储路径方格子
public List mPathPosList = new List();
private static AStarAlgorithm _instance;
public static AStarAlgorithm GetInsatnce
{
get
{
if (_instance == null)
{
_instance = new AStarAlgorithm();
}
return _instance;
}
}
public AStarAlgorithm()
{
InitPoint();
}
//在网格上设置点的信息
private void InitPoint()
{
for (int i = 0; i < mGridWidth; i++)
{
for (int j = 0; j < mGridHeight; j++)
{
mPointGrid[i j] = new AStarPoint(i j);
}
}
//设置障碍物
mPointGrid[4 2].mIsObstacle = true;
mPointGrid[4 3].mIsObstacle = true;
mPointGrid[4 4].mIsObstacle = true;
mPointGrid[4 5].mIsObstacle = true;
mPointGrid[4 6].mIsObstacle = true;
mPointGrid[9 2].mIsObstacle = true;
mPointGrid[9 3].mIsObstacle = true;
mPointGrid[9 4].mIsObstacle = true;
mPointGrid[9 5].mIsObstacle = true;
mPointGrid[9 6].mIsObstacle = true;
mPointGrid[14 2].mIsObstacle = true;
mPointGrid[14 3].mIsObstacle = true;
mPointGrid[14 4].mIsObstacle = true;
mPointGrid[14 5].mIsObstacle = true;
mPointGrid[14 6].mIsObstacle = true;
//显示障碍物
for (int x = 0; x < mGridWidth; x++)
{
for (int y = 0; y < mGridHeight; y++)
{
if (mPointGrid[x y].mIsObstacle)
{
CreatePath(x y Color.blue);
}
}
}
}
public void ClearGrid()
{
for (int x = 0; x < mGridWidth; x++)
{
for (int y = 0; y < mGridHeight; y++)
{
if (!mPointGrid[x y].mIsObstacle)
{
if (mPointGrid[x y].mGameobject != null)
{
Gameobject.Destroy(mPointGrid[x y].mGameobject);
mPointGrid[x y].mGameobject = null;
//重新设置父节点
mPointGrid[x y].mParentPoint = null;
}
}
}
}
}
//寻路
public List FindPath(AStarPoint mStartPoint AStarPoint mEndPoint)
{
if (mEndPoint.mIsObstacle || mStartPoint.mPosition == mEndPoint.mPosition)
{
return null;
}
//开启列表
List openPointList = new List();
//关闭列表
List closePointList = new List();
openP
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-11-03 23:46 AStarXunLuSuanFa\
目录 0 2017-11-03 23:23 AStarXunLuSuanFa\.vs\
目录 0 2017-11-03 23:23 AStarXunLuSuanFa\.vs\AStarXunLuSuanFa\
目录 0 2017-11-03 23:46 AStarXunLuSuanFa\.vs\AStarXunLuSuanFa\v15\
文件 33792 2017-11-03 23:46 AStarXunLuSuanFa\.vs\AStarXunLuSuanFa\v15\.suo
目录 0 2017-11-03 16:11 AStarXunLuSuanFa\.vs\Game AI Programming Project\
目录 0 2017-11-03 16:11 AStarXunLuSuanFa\.vs\Game AI Programming Project\v15\
文件 97280 2017-11-03 16:09 AStarXunLuSuanFa\.vs\Game AI Programming Project\v15\.suo
目录 0 2017-11-03 16:11 AStarXunLuSuanFa\.vs\Game AI Programming Project\v15\sqlite3\
文件 663552 2017-11-03 16:09 AStarXunLuSuanFa\.vs\Game AI Programming Project\v15\sqlite3\storage.ide
目录 0 2017-11-03 23:46 AStarXunLuSuanFa\Assets\
文件 433664 2017-11-03 16:26 AStarXunLuSuanFa\Assets\AStar寻路.doc
文件 175 2017-11-03 17:22 AStarXunLuSuanFa\Assets\AStar寻路.doc.me
文件 449560 2017-11-03 16:09 AStarXunLuSuanFa\Assets\AStar寻路.pdf
文件 175 2017-11-03 16:09 AStarXunLuSuanFa\Assets\AStar寻路.pdf.me
目录 0 2017-11-03 16:11 AStarXunLuSuanFa\Assets\Materials\
文件 191 2017-11-03 16:09 AStarXunLuSuanFa\Assets\Materials.me
文件 5016 2017-11-03 16:09 AStarXunLuSuanFa\Assets\Materials\Black.mat
文件 207 2017-11-03 16:09 AStarXunLuSuanFa\Assets\Materials\Black.mat.me
文件 5016 2017-11-03 16:09 AStarXunLuSuanFa\Assets\Materials\Plane.mat
文件 208 2017-11-03 16:09 AStarXunLuSuanFa\Assets\Materials\Plane.mat.me
目录 0 2017-11-03 16:11 AStarXunLuSuanFa\Assets\MyTextProject\
文件 192 2017-11-03 16:09 AStarXunLuSuanFa\Assets\MyTextProject.me
目录 0 2017-11-03 23:46 AStarXunLuSuanFa\Assets\MyTextProject\Scenes\
文件 192 2017-11-03 16:09 AStarXunLuSuanFa\Assets\MyTextProject\Scenes.me
文件 15512 2017-11-03 23:46 AStarXunLuSuanFa\Assets\MyTextProject\Scenes\AStar.unity
文件 175 2017-11-03 16:09 AStarXunLuSuanFa\Assets\MyTextProject\Scenes\AStar.unity.me
文件 18496 2017-11-03 16:09 AStarXunLuSuanFa\Assets\MyTextProject\Scenes\FMS.unity
文件 175 2017-11-03 16:09 AStarXunLuSuanFa\Assets\MyTextProject\Scenes\FMS.unity.me
目录 0 2017-11-03 16:11 AStarXunLuSuanFa\Assets\MyTextProject\sc
文件 192 2017-11-03 16:09 AStarXunLuSuanFa\Assets\MyTextProject\sc
............此处省略576个文件信息
- 上一篇:stm32f103rct6开发资料 小系统 开发文档
- 下一篇:微风模块9.5
评论
共有 条评论