unity2D生成9*9格子

1.创建一个空对象和格子

2将格子做成预制体(直接将格子拖到这里即可,拖了过后删掉原来的格子)

3.创建脚本并将脚本拖到空对象上

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreateMap : MonoBehaviour
{
    public GameObject tilePrefab; // 你的格子预制体
    public int rows = 9; // 行数
    public int cols = 9; // 列数
    public float spacing = 0.5f; // 格子之间的间距

    void Start()
    {
        CreateGrid();
    }

    void CreateGrid()
    {
        for (int x = 0; x < rows; x++)
        {
            for (int y = 0; y < cols; y++)
            {
                Vector3 position = new Vector3(x * (tilePrefab.transform.localScale.x + spacing), y * (tilePrefab.transform.localScale.y + spacing), 0);
                Instantiate(tilePrefab, position, Quaternion.identity, transform);
            }
        }
    }

}

4.将预制体拖到这里

5.运行游戏就可以看到9*9的格子了(可以调整摄像机的位置让画面显示得更全面)

相关推荐
Scout-leaf2 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530143 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools4 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的4 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21884 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi4 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言