英文:
How to create a checkerboard tile pattern in Unity?
问题
我正在尝试在Unity中创建一个棋盘状的交替瓷砖,但手工创建非常耗时且繁琐。
我尝试创建一个规则瓷砖来实现棋盘效果,但由于规则瓷砖不允许检查邻近瓷砖是否为特定类型,所以没有成功。
如何通过自动瓷砖来实现棋盘效果,以便自动放置黑色和白色瓷砖,交替安排它们?
英文:
I'm trying to create a checkerboard alternating tile in Unity, but creating it by hand is time-consuming and cumbersome.
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?
答案1
得分: 0
以下是翻译好的部分:
使用 System.Collections;
使用 System.Collections.Generic;
使用 UnityEngine;
使用 UnityEngine.Tilemaps;
使用 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
}
希望这对您有所帮助。如果您有任何其他问题,请随时提出。
英文:
Final script
If you're already familiar with all the steps how to make this work inside Unity, here's the script:
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
}
Explanation
To create the desired checkerboard tile rule in Unity, first, create a new C# script and call it something like AlternatingTile.cs
.
Open the script and change the base class to Tile
.
public class AlternatingTile : Tile
Delete the Start()
and Update()
methods, because we are not using MonoBehaviour
.
Add a class-level Sprite[]
array for the "black and white" tiles.
[SerializeField] private Sprite[] tiles;
Create an override
function for Tile.GetFileData
.
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.
#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
Usage
If you followed the same menu item path as in the example above - in Unity Editor, click Assets > Create > 2D > Tiles > Alternating Tile
Select the asset in your Project
window, and assign the two tiles you wish to alternate.
Drag the asset to your Tile Palette and start painting it. As you can see, the tiles are drawn in alternating pattern (checkered/checkerboard pattern).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论