Unity 之 手把手教你实现自己Unity2D游戏寻路逻辑 文末源码

Posted 陈言必行

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 之 手把手教你实现自己Unity2D游戏寻路逻辑 文末源码相关的知识,希望对你有一定的参考价值。

Unity 之 手把手教你实现自己Unity2D游戏寻路逻辑 【文末源码】

前言

还在看别人的寻路逻辑?保姆级教程,一步步教你实现网格寻路逻辑。 超级详细的代码注释,图文步骤详解。写文不易,有帮助的话三连支持下吧~

一,效果展示


二,场景搭建

以一个 9 * 9 的地图为例:

  1. 新建工程,设置屏幕分辨率为: [1080 * 1920],如下图:
    屏幕尺寸

  2. 在自带场景下创建Image作为背景(在Hierarchy右键 --> UI --> Image); 修改其名字为”GridManager“,坐标为: [0, 0, 0],大小为:[1000 * 1000] 并将颜色设置为黑色,完成后效果如下图:

  3. 在"GridManager"下面创建一个空物体,命名为"Grid",将其锚点设置为铺满,并为其添加组件Grid Layout Group,完成后效果如下图:

  4. 在"Grid"下面添加一个Image,并为其添加Button组件,作为点击格子使用,然后Ctrl + D 复制80个,实现效果如下:

  5. 调整"Grid"的Grid Layout Group组件属性值,Left,Top,SpacingX,Y 均设置为10,意思为左边距为10,上边距为10,物体间隔为10,实现后效果如下:

现在这样就已经模拟搭建出类似棋盘场景了,下面看下代码改如何是实现的吧。


三,代码逻辑

  • 寻路二维数组的物体:
/// <summary>
/// 移动方向
/// </summary>
public enum Direction
{
    up, down, left, right
}

public class RoutingObject : MonoBehaviour
{
    /// <summary>
    /// x坐标
    /// </summary>
    public int x;

    /// <summary>
    /// y坐标
    /// </summary>
    public int y;

    /// <summary>
    /// 目标距离
    /// </summary>
    public int targetDistance;

    /// <summary>
    /// 移动距离
    /// </summary>
    public int moveDistance;

    /// <summary>
    /// A*和值(目标距离+移动距离)
    /// </summary>
    public int moveSum;

    /// <summary>
    /// 是否可以移动
    /// </summary>
    public bool isCanMove;

    /// <summary>
    /// 移动方向
    /// </summary>
    public Direction direction;
}
  • 寻路逻辑:根据传入参数(起始点,结束点,地图),进行查找可移动路线,最后筛选出最短路线。部分代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 寻路
/// </summary>
public class Routing
{
    #region 单例

    Routing() { }

    static Routing instance;

    public static Routing Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Routing();
            }
            return instance;
        }
    }

    #endregion
    
    /// <summary>
    /// 二维数组的地图
    /// </summary>
    RoutingObject[,] map;

    /// <summary>
    /// 存储被考虑来寻找最短路径的点
    /// </summary>
    List<RoutingObject> open = new List<RoutingObject>();

    /// <summary>
    /// 存储不再被考虑寻找最短路径的点
    /// </summary>
    List<RoutingObject> closed = new List<RoutingObject>();

    /// <summary>
    /// 存储路线点的列表
    /// </summary>
    List<RoutingObject> route = new List<RoutingObject>();

    /// <summary>
    /// 初始化
    /// </summary>
    void Init(RoutingObject[,] mapArray)
    {
        open.Clear();
        closed.Clear();
        route.Clear();
        map = mapArray;
    }

    /// <summary>
    /// 判断从起始点是否能到达目标点
    /// </summary>
    /// <param name="start_x">起始点x坐标</param>
    /// <param name="start_y">起始点y坐标</param>
    /// <param name="end_x">目标点x坐标</param>
    /// <param name="end_y">目标点y坐标</param>
    /// <param name="map"></param>
    /// <returns></returns>
    public bool IsRouting(RoutingObject start, RoutingObject end, RoutingObject[,] mapArray)
    {
        Init(mapArray);

        Explore(start, end, start);

        // 判断存储路线点的列表里是否存有点
        return route.Count > 0;
    }

    /// <summary>
    /// 探索中心点上下左右四个方向点
    /// </summary>
    void Explore(RoutingObject center, RoutingObject end, RoutingObject start)
    {
        // 中心点不再考虑寻找路径
        closed.Add(center);

        // 将中心点从寻找列表中移除
        if (open.Contains(center))
        {
            open.Remove(center);
        }

        // 是否找到目标点
        if (IsGetEnd(end))
        {
            // 找到目标点
            ReturnRoute(end, start);
        }
        else
        {
            // 判断中心点上边的点
            if (center.y - 1 >= 0)
            {
                RoutingObject up = map[center.x, center.y - 1];
                GetMoveSumByDirection(up, center, end, Direction.up);
            }

            // 判断中心点下边的点
            if (center.y + 1 < GridManager.Instance.mapColumnCount)
            {
                RoutingObject down = map[center.x, center.y + 1];
                GetMoveSumByDirection(down, center, end, Direction.down);
            }

            // 判断中心点左边的点
            if (center.x - 1 >= 0)
            {
                RoutingObject left = map[center.x - 1, center.y];
                GetMoveSumByDirection(left, center, end, Direction.left);
            }

            // 判断中心点右边的点
            if (center.x + 1 < GridManager.Instance.mapRowCount)
            {
                RoutingObject right = map[center.x + 1, center.y];
                GetMoveSumByDirection(right, center, end, Direction.right);
            }

            if (open.Count > 0)
            {
                // 没有找到目标点,则在被考虑的列表中找出一个和值最小的
                RoutingObject ro = GetMinimumMoveSum();
                Explore(ro, end, start);
            }
            else
            {
                Debug.Log("没有找到目标点");
            }
        }
    }

    /// <summary>
    /// 根据传进来的方向去获取和值
    /// </summary>
    /// <param name="center"></param>
    /// <param name="start"></param>
    /// <param name="end"></param>
    /// <param name="direction"></param>
    void GetMoveSumByDirection(RoutingObject center, RoutingObject start, RoutingObject end, Direction direction)
    {
        // 判断这个点是否能移动或者是否被考虑
        if (IsForward(center))
        {
            center.direction = direction;
            // 获取移动距离
            center.moveDistance = GetDistance(center, start);
            // 获取目标距离
            center.targetDistance = GetDistance(center, end);
            // 获取A*和值
            center.moveSum = center.moveDistance + center.targetDistance;
            // 将中心点加入将要被考虑的列表中
            open.Add(center);
        }
        else
        {
            //Debug.Log(center.name + " 不能移动");
        }
    }

    /// <summary>
    /// 判断这个点是否属于未来被考虑前进的点
    /// </summary>
    /// <param name="ro"></param>
    /// <returns></returns>
    bool IsForward(RoutingObject ro)
    {
        // 判断这个点是否已经在不再考虑的列表中
        if (closed.Contains(ro) || open.Contains(ro))
        {
            return false;
        }
        else
        {
            // 判断这个点是否可以移动
            if (ro.isCanMove)
            {
                return true;
            }
            else
            {
                // 不可以移动就加入不再考虑的列表中
                closed.Add(ro);
                return false;
            }
        }
    }

    /// <summary>
    /// 获取距离
    /// </summary>
    /// <param name="start"></param>
    /// <param name="end"></param>
    int GetDistance(RoutingObject start, RoutingObject end)
    {
        // 定义目标距离返回值, --> 谁大,谁减谁
        return Mathf.Abs(start.x - end.x) + Mathf.Abs(start.y - end.y);
    }

    /// <summary>
    /// 是否找到目标点
    /// </summary>
    /// <returns></returns>
    bool IsGetEnd(RoutingObject end)
    {
        return closed.Contains(end);
    }

    /// <summary>
    /// 在被考虑的列表中获取和值最小的点
    /// </summary>
    /// <returns></returns>
    RoutingObject GetMinimumMoveSum()
    {
        RoutingObject ro = null;
        RoutingObject temporary = new RoutingObject();
        for (int i = 0; i < open.Count; i++)
        {
            //Debug.Log("当前 " + open[i].name + " 的和值为: " + open[i].moveSum);
            // 列表中的第一个不需要比较,直接赋值
            if (i == 0)
            {
                ro = open[i];
                temporary = open[i];
            }
            else
            {
                // 寻找列表中和值最小的点
                if (open[i].moveSum < temporary.moveSum)
                {
                    ro = open[i];
                    temporary = open[i];
                }
            }
        }
        //Debug.Log("最终 " + ro.name + " 的和值为: " + ro.moveSum);
        return ro;
    }


    /// <summary>
    /// 返回路线
    /// </summary>
    /// <param name="center"></param>
    /// <param name="start"></param>
    void ReturnRoute(RoutingObject center, RoutingObject start)
    {
        // 将这个点存储到路线列表中
        route.Add(center);
        // 判断路线列表中是否包含起始点
        if (!route.Contains(start))
        {
            // 没有包含
            // 返回路线取这个点的反方向
            switch (center.direction)
            {
                case Direction.up:
                    ReturnRoute(map[center.x, center.y + 1], start);
                    break;
                case Direction.down:
                    ReturnRoute(map[center.x, center.y - 1], start);
                    break;
                case Direction.left:
                    ReturnRoute(map[center.x + 1, center.y], start);
                    break;
                case Direction.right:
                    ReturnRoute(map[center.x - 1, center.y], start);
                    break;
            }

        }
        else
        {
            RouteSort(start);
        }
    }

    /// <summary>
    /// 路线排序(将起始点从存储路线点的列表中移除,并从起始点到目标点重新排序)
    /// </summary>
    void RouteSort(RoutingObject start)
    {
        List<RoutingObject> list = new List<RoutingObject>(route);

        route.Clear();

        for (int i = list.Count - 1; i >= 0; i--)
        {
            if (list[i] != start)
            {
                route.Add(list[i]);
            }
        }
    }

    /// <summary>
    /// 返回最短路线
    /// </summary>
    /// <returns></returns>
    public List<RoutingObject> GetRoute()
    {
        return route;
    }
}


  • 创建GridManager脚本,将其挂载到GridManager物体上,
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GridManager : MonoBehaviour
{
    /// <summary>
    /// 单例
    /// </summary>
    public static GridManager Instance;
    
    /// <summary>
    /// 地图行数
    /// </summary>
    public int mapColumnCount = 9;
    /// <summary>
    /// 地图列数
    /// </summary>
    public int mapRowCount = 9;

    /// <summary>
    /// 当前可移动最短路径存储集合
    /// </summary>
    public List<RoutingObject> routeList = new List<RoutingObject>();

    /// <summary>
    /// 已被占领的格子集合 -- 格子上有障碍物
    /// </summary>
    public List<GameObject> OccupyGridList = new List<GameObject>();

    /// <summary>
    /// 存储地图格子
    /// </summary>
    private GameObject[,] GridArray;
    
    /// <summary>
    /// 当前所选格子
    /// </summary>
    private GameObject selectGrid;
    
    private void Awake()
    {
        Instance = this;
        GridArray = new GameObject[mapRowCount, mapColumnCount];
    }

    void Start()
    {
    
    }

    /// <summary>
    /// 每个格子初始化时 调用赋值
    /// </summary>
    /// <param name="go">格子</param>
    /// <param name="x">所在X</param>
    /// <param name="y">所在Y</param>
    public void SetGridArray(GameObject go, int x, int y)
    {
        GridArray[x, y] = go;
    }
    
    /// <summary>
    /// 根据(x,y)获取 格子物体
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <returns></returns>
    GameObject GetGridArray(int x, int y)
    {
        return GridArray[x, y];
    }
    
    /// <summary>
    /// 获取一个准备移动球的二维数组(每个坐标点上记录着是否可以移动)
    /// </summary>
    /// <returns></returns>
    Grid[,] GetMoveMap()
    {
        // 定义存储地图格子是否可以移动的二维数组
        Grid[,] array = new Grid[mapRowCount, mapColumnCount];

        for (int i = 0; i < mapRowCount; i++)
        {
            for (int j = 0; j < mapColumnCount; j++)
            {
                if (OccupyGridList.Contains(GridArray[i, j]))
                {
                    GridArray[i, j].GetComponent<Grid>().isCanMove = false;
                }
                else
                {
                  GridArray[i, j].GetComponent<Grid>().isCanMove = true;
                }
                array[i, j] = GridArray[i, j].GetComponent<Grid>();
            }
        }
        return array;
    }
    
    /// <summary>
    /// 点击格子的调用
    /// </summary>
    /// <param name="selectObj"></param>
    public void OnClickGrid(GameObject selectObj)
    {
        if (selectGrid == null)
        {
            selectGrid = selectObj;
        }
        else
        {
            // 获取当前地图(地图记录每个点是否能移动)
            RoutingObject[,] map = GetMoveMap();
            // 获取起始点
            RoutingObject start = selectGrid.GetComponent<RoutingObject>();
            RoutingObject end = selectObj.GetComponent<RoutingObject>();
            // 判断是否可以通过
            if (Routing.Instance.IsRouting(start, end, map))
            {
                Debug.Log("判断可以通过");
                
                // 标识为起点
                start.gameObject.GetComponent以上是关于Unity 之 手把手教你实现自己Unity2D游戏寻路逻辑 文末源码的主要内容,如果未能解决你的问题,请参考以下文章

小松教你手游开发unity系统模块开发热更

小松教你手游开发unity实用技能计算目标物体是否在自己的扇形视野范围

小松教你手游开发unity实用技能unity ios快捷打包

小松教你手游开发unity实用技能unity所有特殊文件夹的用途(转自雨松momo)

小松教你手游开发unity实用技能角色头部跟随镜头旋转

小松教你手游开发unity实用技能往avatar身边放置一个物体(随机)