Unity3d中也内嵌了TileMap的功能,不用再使用第三方的TileMap了。今天就尝试了下使用TileMap制作地图。
创建TileMap
在Hierarchy视图右键 2D > TileMap 创建一个地图节点对象。此节点会包含Grid和TileMap组件,如下:

这里提供了几种模式的Tilemap,我这里就选择矩形挂块地图。
编辑TileMap
创建完一个TileMap节点,但是场景中什么都没有,这个时候,我们需要在Package Manager中安装 2D Tilemap Editor插件,安装完成之后,我们选择Grid对象,就能在Scene窗口中间距网格了。
为了能在上面绘制Tile,我们需要创建一个Tile Palette,相当于绘画的调色板,Tilemap对象就是画布。我们需要在Window > 2D > Tile Patette 打开此窗口(需要安装了上面说的插件才能看见此菜单选项)。

如果还没有Palette,我们需要创建一个,点击上面的create new Palette 按钮,根据弹窗提示,创建即可,创建完成后,我们能在Create New Palette这个下来菜单看见创建的新Palette,开始创建出来的Palette是没有内容,我们需要把Sprite拖入上面窗口,就能帮我们创建出瓦块了。

我这里拉了几个Sprite 进来,看起就如上面效果,现在我们可以选中Tilemap节点,Active Tilemap就会显示我们当前选中的tilemap,我们可以选中上面的画笔,矩形攻击,然后选中一个下面的Tile,就能在scene上进行绘制了。

多层Tilemap
默认是创建一个Tilemap,就相当于一层,我们可以手动在Grid下面创建新的Tilemap节点,加上Tilemap和TilemapRenderer组件即可,编辑的时候选中那一层就会在编辑这层上绘制Tile ,互不干涉。
添加碰撞框
比如地形都是存在碰撞的,我可选中Tilemap节点,添加Tilemap Collider 2D组件,这样单层的全部添加了碰撞了,显示上回出现绿色的框表示。

刷预制体对象
我们需要下载一个2d-extras插件来扩展我们Tilemap Editor的功能,下载连接
https://github.com/Unity-Technologies/2d-extras
下载的时候注意我们选中的分支是否支持当前Unity的版本,否正存在编译错误。
把此插件安装起,我们的编辑工具就可以存在多种刷子了

平常就存在一个Default Brush。我们能使用GameObject Brush来绘制Prefab对象,比如我们需要交互的人啊,动物啊这些单位对象就非常合适。
程序控制
我们也能通过代码来访问每个格子的内容,我们需要得到Grid组件和tileMap组件。
Grid组件提供把世界坐标能转换成Grid坐标,我们能用Grid最标通过tileMap组件获取当前cell的tile名字,或则设置当前Cell的tile。
//Grid.cs
namespace UnityEngine
{
public sealed class Grid : GridLayout
{
public Grid();
public Vector3 cellSize { get; set; }
public Vector3 cellGap { get; set; }
public CellLayout cellLayout { get; set; }
public CellSwizzle cellSwizzle { get; set; }
public static Vector3 InverseSwizzle(CellSwizzle swizzle, Vector3 position);
public static Vector3 Swizzle(CellSwizzle swizzle, Vector3 position);
public Vector3 GetCellCenterLocal(Vector3Int position);
public Vector3 GetCellCenterWorld(Vector3Int position);
}
}
//Tilemap
namespace UnityEngine.Tilemaps
{
public sealed class Tilemap : GridLayout
{
public Tilemap();
public Grid layoutGrid { get; }
public Vector3 tileAnchor { get; set; }
public Vector3Int origin { get; set; }
public Orientation orientation { get; set; }
public Color color { get; set; }
public Vector3Int size { get; set; }
public float animationFrameRate { get; set; }
public Matrix4x4 orientationMatrix { get; set; }
public Vector3Int editorPreviewOrigin { get; }
public Vector3Int editorPreviewSize { get; }
public BoundsInt cellBounds { get; }
public Bounds localBounds { get; }
public static event Action<Tilemap, SyncTile[]> tilemapTileChanged;
public void AddTileFlags(Vector3Int position, TileFlags flags);
public void BoxFill(Vector3Int position, TileBase tile, int startX, int startY, int endX, int endY);
public bool ContainsTile(TileBase tileAsset);
public void EditorPreviewBoxFill(Vector3Int position, Object tile, int startX, int startY, int endX, int endY);
public void EditorPreviewFloodFill(Vector3Int position, TileBase tile);
public void FloodFill(Vector3Int position, TileBase tile);
public Vector3 GetCellCenterLocal(Vector3Int position);
public Vector3 GetCellCenterWorld(Vector3Int position);
public Tile.ColliderType GetColliderType(Vector3Int position);
public Color GetColor(Vector3Int position);
public Color GetEditorPreviewColor(Vector3Int position);
public Sprite GetEditorPreviewSprite(Vector3Int position);
public TileBase GetEditorPreviewTile(Vector3Int position);
public T GetEditorPreviewTile<T>(Vector3Int position) where T : TileBase;
public TileFlags GetEditorPreviewTileFlags(Vector3Int position);
public Matrix4x4 GetEditorPreviewTransformMatrix(Vector3Int position);
public GameObject GetInstantiatedObject(Vector3Int position);
public Sprite GetSprite(Vector3Int position);
public TileBase GetTile(Vector3Int position);
public T GetTile<T>(Vector3Int position) where T : TileBase;
public TileFlags GetTileFlags(Vector3Int position);
public TileBase[] GetTilesBlock(BoundsInt bounds);
public Matrix4x4 GetTransformMatrix(Vector3Int position);
public int GetUsedTilesCount();
public int GetUsedTilesNonAlloc(TileBase[] usedTiles);
public bool HasEditorPreviewTile(Vector3Int position);
public bool HasTile(Vector3Int position);
public void RefreshAllTiles();
public void RefreshTile(Vector3Int position);
public void RemoveTileFlags(Vector3Int position, TileFlags flags);
public void ResizeBounds();
public void SetColliderType(Vector3Int position, Tile.ColliderType colliderType);
public void SetColor(Vector3Int position, Color color);
public void SetEditorPreviewColor(Vector3Int position, Color color);
public void SetEditorPreviewTile(Vector3Int position, TileBase tile);
public void SetEditorPreviewTransformMatrix(Vector3Int position, Matrix4x4 transform);
public void SetTile(Vector3Int position, TileBase tile);
public void SetTileFlags(Vector3Int position, TileFlags flags);
public void SetTiles(Vector3Int[] positionArray, TileBase[] tileArray);
public void SetTilesBlock(BoundsInt position, TileBase[] tileArray);
public void SetTransformMatrix(Vector3Int position, Matrix4x4 transform);
public void SwapTile(TileBase changeTile, TileBase newTile);
1 引用通告