Unity2D游戏开发记录 UI小地图的实现

本文详细介绍了如何在Unity2D游戏中实现小地图,包括地图生成方法、数据结构(如HashSet和int[,]数组)、纹理处理和更新逻辑。作者分享了自己的实现代码,并对比了与视频中的不同之处,希望能帮助其他开发者。
摘要由CSDN通过智能技术生成

Unity2D游戏开发记录 UI小地图的实现

核心思路来源

链接: 【Unity 2D】小地图的实现思路
代码展示在14分40秒

我的实现方法

地图生成方法
教程
链接:【Unity教程搬运】Unity中程序化生成的2D地牢

地图采用的 数据格式以及使用方法

// Hashset<Vector2Int> floor
floor.add(new Vector2Int(int x,inty));
//获取地图信息
foreach(var position in floor)
{
print(position.x,position.y)
}

代码

public class minimap : MonoBehaviour
{
    //小地图大小
   static Vector2Int mapsize = new Vector2Int(100,100);
    private Image mapImage;

    private Texture2D tex;
    //玩家探索过的区域
    private int[,] visit;
    //地图
    private int[,] map;

    [SerializeField]
    public Color color;
    public HashSet<Vector2Int> floor;
    public Transform player;
    private void Awake()
    {
        map = new int[100, 100];
        mapImage = GetComponent<Image>();
        tex = new Texture2D(100, 100);
        visit = new int[100, 100];
    }
    public void  LateUpdate()
    {
        if (GameObject.Find("Generator").GetComponent<RoomFirstDungonGenerator>().floor != null)
            floor = GameObject.Find("Generator").GetComponent<RoomFirstDungonGenerator>().floor;
        else
            floor = new HashSet<Vector2Int>();
        // player = GameObject.Find("Player").GetComponent<Transform>();
        ShowMinimap();
       // ShowMinimap1();
    }
   
    /// <summary>
    /// tex.SetPixel(int x,int y,Color color);
    /// </summary>
    public void ShowMinimap()
    {
        tex = new Texture2D(100,100);
        int x = (int)(player.position.x);
        int y = (int)(player.position.y);
        for (int i = -5; i < 5; i++)
        {
            for(int j = -5; j < 5; j++)
            {
                if ((x + i) >= 0 && (x + i) < mapsize.x && (y + j) >= 0 && (y + j) < mapsize.y)
                {
                    visit[(x + i), (y + j)] = 1;
                }
            }
        }
        foreach(var flor in floor)
        {
            map[flor.x, flor.y] = 1;
        }
      for(int i = 0; i < mapsize.x; i++)
        {
            for(int j = 0; j < mapsize.y; j++)
            {
                if (i == 0 || j == 0 || i == mapsize.x - 1 || j == mapsize.y) 
                {
                    tex.SetPixel(i, j, Color.white);
                    continue;
                }
                 x = (int)(player.position.x)  - mapsize.x / 2 + i;
                 y = (int)(player.position.y) - mapsize.y / 2 + j;
                //x[-50,150]
                if (x > 0 && x < mapsize.x - 1 && y > 0 && y < mapsize.y - 1)  {
                    if (visit[x, y] == 1)
                    {
                        if (map[x, y] == 1)
                        {
                            tex.SetPixel(i, j, Color.blue);
                        }
                        else
                        {
                            tex.SetPixel(i, j, new Color(0.5f, 0.5f, 0.5f, 0.5f));
                        }

                    }
                    else
                    {
                        tex.SetPixel(i, j, new Color(0.2f, 0.2f, 0.2f, 0.5f));
                    }
                }
                else
                {
                    tex.SetPixel(i, j, new Color(0.2f, 0.2f, 0.2f, 0.5f));
                }
            }
        }
        
        tex.Apply();
        Sprite s = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
        mapImage.sprite = s;

    }

将脚本挂载在Image组件上
在这里插入图片描述

在这里插入图片描述
效果图

补充说明

地图分4种颜色
可以行走的区域为蓝色
处于map之外的颜色为Color(0.5f, 0.5f, 0.5f)
未探索的区域以及地图以外的区域为Color(0.2f, 0.2f, 0.2f)

由于不透明度的原因,未探索的区域以及地图以外的区域显示为背景黑色

https://blog.csdn.net/iningwei/article/details/88537706
Texture2D与Sprite之间转换

我的实现方法还有些与视频不同之处,视频作者在小地图的制作中使用了协程的技术
以及在细节上,玩家在地图中心点为绿色等等…
希望我的文章能帮助到大家

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值