[英]How to create an alternating rule tile in Unity?
I'm trying to create a checkerboard tile in Unity, but creating it by hand is time-consuming and cumbersome.我正在尝试在 Unity 中创建一个棋盘图块,但手动创建它既费时又麻烦。
I tried creating a rule tile to achieve the checkered effect, but it hasn't worked since the rule tile doesn't allow checking if the neighbor tile is of a specific type.我尝试创建一个规则图块来实现方格效果,但它没有奏效,因为规则图块不允许检查相邻图块是否属于特定类型。
How can I achieve a checkerboard effect with automatic tiles to automatically place black and white tiles respectively - alternate between them?如何使用自动瓷砖实现棋盘效果以分别自动放置黑色和白色瓷砖 - 在它们之间交替?
If you're already familiar with all the steps how to make this work inside Unity, here's the script:如果您已经熟悉如何在 Unity 中执行此操作的所有步骤,请使用以下脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEditor;
public class AlternatingTile : Tile
{
[SerializeField] private Sprite[] tiles;
public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
{
uint mask = (uint) (position.x + position.y);
tileData.sprite = tiles[mask % 2];
}
#if UNITY_EDITOR
[MenuItem("Assets/Create/2D/Tiles/Alternating Tile")]
public static void CreateAlternatingTile()
{
string path = EditorUtility.SaveFilePanelInProject(
"Alternating Tile",
"New Alternating Tile",
"Asset",
"Please enter a name for the new alternating tile",
"Assets");
AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<AlternatingTile>(), path);
}
#endif
}
To create the desired checkerboard tile rule in Unity, first, create a new C# script and call it something like AlternatingTile.cs
.要在 Unity 中创建所需的棋盘格规则,首先,创建一个新的 C# 脚本并将其命名为类似AlternatingTile.cs
名称。
Open the script and change the base class to Tile
.打开脚本并将 base class 更改为Tile
。
public class AlternatingTile : Tile
Delete the Start()
and Update()
methods, because we are not using MonoBehaviour
.删除Start()
和Update()
方法,因为我们没有使用MonoBehaviour
。
Add a class-level Sprite[]
array for the "black and white" tiles.为“黑白”图块添加一个类级别的Sprite[]
数组。
[SerializeField] private Sprite[] tiles;
Create an override
function for Tile.GetFileData
.为Tile.GetFileData
创建override
function 。
public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
{
uint mask = (uint) (position.x + position.y); // gets the absolute position of the tile
tileData.sprite = tiles[mask % 2]; // mask = 0 or 1 depending on the absolute position of the tile
}
Now that we created the logic for handling which tiles to draw, let's enable Unity to create the asset for this tile.现在我们已经创建了处理绘制哪些图块的逻辑,让 Unity 为这个图块创建资产。
#if UNITY_EDITOR
[MenuItem("Assets/Create/2D/Tiles/Alternating Tile")] // Where to put the menu item in menu
public static void CreateAlternatingTile()
{
string path = EditorUtility.SaveFilePanelInProject(
"Alternating Tile", // name of the menu
"New Alternating Tile", // default name of the new file
"Asset", // extension (.Asset)
"Please enter a name for the new alternating tile", // message
"Assets/Sprites & Tiles/Tiles"); // default folder in the project - change this to match your project!
AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<AlternatingTile>(), path);
}
#endif
If you followed the same menu item path as in the example above - in Unity Editor, click Assets > Create > 2D > Tiles > Alternating Tile
如果您遵循与上例相同的菜单项路径 - 在 Unity Editor 中,单击Assets > Create > 2D > Tiles > Alternating Tile
Select the asset in your Project
window, and assign the two tiles you wish to alternate. Select Project
中的资产 window,并分配您希望交替的两个图块。
Drag the asset to your Tile Palette and start painting it.将资产拖到您的 Tile Palette 并开始绘制它。 As you can see, the tiles are drawn in alternating pattern (checkered/checkerboard pattern).如您所见,瓷砖以交替图案(方格/棋盘图案)绘制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.