原地址: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. [问题] PHP接收Request payload传递过来的参数

    在使用Apidoc的时候,有一个配置参数是 @apiSampleRequest http://localhost/api 在测试时,发现发送的参数是Request payload的,而PHP中使用的A ...

  2. css关于宽度

    很多时候,我们指定了某个元素的宽度,浏览器渲染时却只给这个元素一半的宽度,这时可以试试min-width属性,该属性表示浏览器不能偷懒,资源再紧张也得分配min-width指定的宽度.

  3. asp.net_MVC_jq三级联动

    数据库结构 建立三张表,Association,Team,Player 关系如下: 建立asp.net MVC 3项目,在HomeController.cs中利用Linq to SQL获取数据 首先实 ...

  4. Html5 Video 实现方案

    来源:http://ask.dcloud.net.cn/article/569 源码下载 前言: 最近项目中需要用到html5 视频播放功能,于是稍微研究了解了下,遇到了很多坑,特此记录下. 一. H ...

  5. android开发中R文件丢失

    R文件在android开发中,占据着中会在重要的地位,里面的内容有系统自动生成,不可随意修改,然而在开发过程中,总是不可知的丢失,这里总结一下修补方法   #.在Eclipse里可以         ...

  6. How Indexes Are Stored

    reference:  http://docs.oracle.com/cd/B28359_01/server.111/b28318/schema.htm#CHDJGADJ 当创建索引的时候,Oracl ...

  7. 2016/01/19 javascript学习笔记-name属性

    1. name属性只在少数html元素中有效:包括表单.表单元素.<iframe>和<img>元素. 基于name属性的值选取html元素,可以使用document对象的get ...

  8. Java的基本数据类型

    java的基本数据类型是四类八种: 整型 byte  1字节  8位 short 2字节 16位 int 4字节 32位 long   8字节 64位 在hibernate自动映射中会根据数字长度,选 ...

  9. springmvc学习(四)

    1.使用 @CookieValue 绑定请求中的 Cookie 值 例子: java @RequestMapping(value="/testCookieValue") publi ...

  10. 使用qt制作简单的加法,乘法运算。

    1.首先构架qt应用项目 2.然后打开使用 Qt desinger打开 Fomr File 里的UI文件进行编辑 3.由于此程序只需点击加号,减号这两个按钮,所以设置了两个信号槽 4.然后是连接信号槽 ...