原地址:http://www.unity蛮牛.com/blog-13769-1078.html

首先看了这篇翻译外国人的文章http://www.raywenderlich.com/zh-hans/21503/a%E6%98%9F%E5%AF%BB%E8%B7%AF%E7%AE%97%E6%B3%95%E4%BB%8B%E7%BB%8D

 
 
1.定义地图节点,及初始化地图数据

[code]csharpcode:

using UnityEngine;
using System.Collections.Generic; public class Map
{
/// <summary>
/// 初始化地图
/// </summary>
/// <returns></returns>
public static Dictionary<string, MapInfo> GetMap()
{
Dictionary<string, MapInfo> temp = new Dictionary<string, MapInfo>(); for (int i = ; i < ; i++)
{
string s = "";
for (int j = ; j < ; j++)
{
int tt = ;
if (i > && i < && j == )
{
tt = ;
}
MapInfo mi = new MapInfo(i, j, tt);
temp.Add(i + "-" + j, mi);
s += mi.tag + " ";
}
Debug.Log(s);
}
return temp;
}
} /// <summary>
/// 地图节点
/// </summary>
public class MapInfo
{
/// <summary>
/// X
/// </summary>
public int x;
/// <summary>
/// Y
/// </summary>
public int y;
/// <summary>
/// 是否可行走
/// </summary>
public int tag;
/// <summary>
/// G
/// </summary>
public int gValue;
/// <summary>
/// H
/// </summary>
public int hValue;
/// <summary>
/// 父节点
/// </summary>
public MapInfo parent; public MapInfo()
{ } /// <summary>
/// 构造
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="tag"></param>
public MapInfo(int x, int y,int tag)
{
this.x = x;
this.y = y;
this.tag = tag;
this.gValue = ;
this.hValue = ;
this.parent = null;
}
}

2.由起点终点寻路,有注释,知道原理的话,应该很容易懂

[code]csharpcode:

using UnityEngine;
using System.Collections.Generic; public class AStar : MonoBehaviour
{
/// <summary>
/// 地图
/// </summary>
Dictionary<string, MapInfo> map;
/// <summary>
/// open列表
/// </summary>
Dictionary<string, MapInfo> openList = new Dictionary<string, MapInfo>();
/// <summary>
/// close列表
/// </summary>
Dictionary<string, MapInfo> closeList = new Dictionary<string, MapInfo>(); /// <summary>
/// 当前点
/// </summary>
MapInfo currentV;
/// <summary>
/// 当前点的相邻节点列表
/// </summary>
Dictionary<string, MapInfo> adjancentMap; // Use this for initialization
void Start ()
{
map = Map.GetMap();
MapInfo st = map["5-2"];//start
MapInfo end = map["6-8"];//end FindPath(st, end);
} /// <summary>
/// 寻路
/// </summary>
/// <param name="start">起点</param>
/// <param name="end">终点</param>
public void FindPath(MapInfo start,MapInfo end)
{
openList.Add(start.x + "-" + start.y, start); do
{
currentV = GetTheLowestFrom(openList); closeList.Add(currentV.x + "-" + currentV.y, currentV);
openList.Remove(currentV.x + "-" + currentV.y); if (closeList.ContainsKey(end.x + "-" + end.y))
{
Debug.Log("FindPath"); PrintThePath(end);
break;
} adjancentMap = AdjacentList(currentV); foreach (string k in adjancentMap.Keys)
{
if (closeList.ContainsKey(k))
{
continue;
} if (!openList.ContainsKey(k))
{
adjancentMap[k].parent = currentV;
adjancentMap[k].gValue = currentV.gValue + ;
adjancentMap[k].hValue = GetManhattanDistance(adjancentMap[k], end);
openList.Add(k, adjancentMap[k]);
}
else
{
int g = currentV.gValue + ;
if (g < adjancentMap[k].gValue)
{
adjancentMap[k].gValue = g;
adjancentMap[k].parent = currentV;
}
}
} } while (openList.Count > );
} /// <summary>
/// 获取openlist中F最小的节点
/// </summary>
/// <param name="open"></param>
/// <returns></returns>
public MapInfo GetTheLowestFrom(Dictionary<string, MapInfo> open)
{
MapInfo result=null;
int min = ;
foreach (MapInfo m in open.Values)
{
if (m.gValue + m.hValue < min)
{
result = m;
min = m.gValue + m.hValue;
}
}
return result;
} /// <summary>
/// 获取当前节点的相邻节点
/// </summary>
/// <param name="m">当前节点</param>
/// <returns></returns>
public Dictionary<string, MapInfo> AdjacentList(MapInfo m)
{
Dictionary<string, MapInfo> resultDic=new Dictionary<string,MapInfo>(); string left = (m.x - ) + "-" + m.y;
string right = (m.x + ) + "-" + m.y;
string top = m.x + "-" + (m.y - );
string bot = m.x + "-" + (m.y + ); if (map.ContainsKey(left))
{
if(map[left].tag==)
resultDic.Add(left, map[left]);
} if (map.ContainsKey(right))
{
if (map[right].tag == )
resultDic.Add(right, map[right]);
} if (map.ContainsKey(top))
{
if (map[top].tag == )
resultDic.Add(top, map[top]);
} if (map.ContainsKey(bot))
{
if (map[bot].tag == )
resultDic.Add(bot, map[bot]);
}
return resultDic;
} /// <summary>
/// 获得两个点的曼哈顿距离
/// 作为估值函数
/// </summary>
/// <param name="st"></param>
/// <param name="end"></param>
/// <returns></returns>
public int GetManhattanDistance(MapInfo st, MapInfo end)
{
int result = ;
result = Mathf.Abs(st.x - end.x) + Mathf.Abs(st.y - end.y);
return result;
} /// <summary>
/// 输出路径
/// </summary>
/// <param name="end">终点</param>
public void PrintThePath(MapInfo end)
{
string s = "";
MapInfo m = end;
while (m.parent != null)
{
s += "("+m.parent.x + "," + m.parent.y + ")->";
m = m.parent;
}
Debug.Log(s);
}
}

Unity C#写的A*寻路的更多相关文章

  1. 关于Unity中网格导航与寻路

    寻路思路 1.烘焙出地形数据,导航数据,区分哪些是路径,哪些是障碍物 2.给要寻路的角色添加寻路的组件,加好了以后就会有速度和目的地之类的参数设置 3.只要设置好目的地,角色就会根据烘焙好的地图自己走 ...

  2. unity自带寻路Navmesh入门教程(三)

    继续介绍NavMesh寻路的功能,接下来阿赵打算讲一下以下两个例子,先看看完成的效果:   第一个例子对于喜欢DOTA的朋友应该很熟悉了,就是不同小队分不同路线进攻的寻路,红绿蓝三个队伍分别根据三条路 ...

  3. 【转】unity自带寻路Navmesh入门教程(三)

    http://liweizhaolili.blog.163.com/blog/static/16230744201271225812998/ 继续介绍NavMesh寻路的功能,接下来阿赵打算讲一下以下 ...

  4. unity3d——自带寻路Navmesh (三)(转)

    继续介绍NavMesh寻路的功能,接下来阿赵打算讲一下以下两个例子,先看看完成的效果:   第一个例子对于喜欢DOTA的朋友应该很熟悉了,就是不同小队分不同路线进攻的寻路,红绿蓝三个队伍分别根据三条路 ...

  5. Unity手游之路<八>自动寻路Navmesh之入门

    http://blog.csdn.net/janeky/article/details/17457533 在的大部分mmo游戏都有了自动寻路功能.点击场景上的一个位置,角色就会自动寻路过去.中间可能会 ...

  6. Unity调用Android方法

    步骤 废话不多说,直接来步骤吧1.创建工程,弄大概像这样一个界面2.在unity中写好代码,像这样,记得给界面绑定好事件啥的 using UnityEngine; using UnityEngine. ...

  7. 极简Unity调用Android方法

    简介 之前写了篇unity和Android交互的教程,由于代码里面有些公司的代码,导致很多网友看不懂,并且确实有点小复杂,这里弄一个极简的版本 步骤 废话不多说,直接来步骤吧 1.创建工程,弄大概像这 ...

  8. Unity Shaders Vertex & Fragment Shader入门

    http://blog.csdn.net/candycat1992/article/details/40212735 三个月以前,在一篇讲卡通风格的Shader的最后,我们说到在Surface Sha ...

  9. unity android 集成指南

    原地址:http://blog.csdn.net/alking_sun/article/details/36175187 1.安卓层开发并暴露接口.   launcher activity(以下称为U ...

随机推荐

  1. 学习GraphX

    首先准备如下社交图形数据:

  2. 【C#4.0图解教程】笔记(第1章~第8章)

    第1章 C#和.NET框架 1..NET框架的组成 .NET框架由三部分组成(严格来说只有CLR和FCL(框架类库)两部分),如图 执行环境称为:CLR(公共语言运行库),它在运行期管理程序的执行. ...

  3. PowerDesigner使用教程 —— 概念数据模型 (转)

    一.概念数据模型概述    概念数据模型也称信息模型,它以实体-联系(Entity-RelationShip,简称E-R)理论为基础,并对这一理论进行了扩充.它从用户的观点出发对信息进行建模,主要用于 ...

  4. Centos7最小化安装后(minimal)安装图形界面

    centos7下载地址:http://mirrors.cqu.edu.cn/CentOS/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso 下载后用vmwa ...

  5. Google maps not working IE11

    参考原因: http://www.easypagez.eu/maps/ieworking.html 如果还不行的话,在map的样式上加上width:100%;height:100% ;position ...

  6. PL/SQL Developer 使用中文条件查询时无数据的解决方法

    PL/SQL Developer 使用中文条件查询时无数据,这是由于字符集的不一致导致的. 执行以下sql命令:select userenv('language') from dual; 显示:SIM ...

  7. NSArray函数

    1.判断是否包含某一个元素,返回1则表示有 - (BOOL)countainsObject:(id)anObject BOOL isContain = [arrayboy containsObject ...

  8. 【mysql】【分组】后取每组的top2

    DROP TABLE IF EXISTS `tb1`; CREATE TABLE `tb1` ( `id` ) NOT NULL AUTO_INCREMENT, `a` ) DEFAULT NULL, ...

  9. css 盒子模型理解

    盒子模型是html+css中最核心的基础知识,理解了这个重要的概念才能更好的排版,进行页面布局.下面是自己积累和总结的关于css盒子模型的知识^_^,希望对初学者有用. 一.css盒子模型概念 CSS ...

  10. [leetcode]最长递增序列

    class Solution { public: int lengthOfLIS(vector<int>& nums) { int n=nums.size(); ) ; vecto ...