如何在Unity中创建棋盘格瓦片图案?

huangapple go评论73阅读模式
英文:

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(&quot;Assets/Create/2D/Tiles/Alternating Tile&quot;)]
    public static void CreateAlternatingTile()
    {
        
        string path = EditorUtility.SaveFilePanelInProject(
            &quot;Alternating Tile&quot;,
            &quot;New Alternating Tile&quot;,
            &quot;Asset&quot;,
            &quot;Please enter a name for the new alternating tile&quot;,
            &quot;Assets&quot;);

        AssetDatabase.CreateAsset(ScriptableObject.CreateInstance&lt;AlternatingTile&gt;(), 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(&quot;Assets/Create/2D/Tiles/Alternating Tile&quot;)] // Where to put the menu item in menu
public static void CreateAlternatingTile()
{
    string path = EditorUtility.SaveFilePanelInProject(
            &quot;Alternating Tile&quot;, // name of the menu
            &quot;New Alternating Tile&quot;, // default name of the new file
            &quot;Asset&quot;, // extension (.Asset)
            &quot;Please enter a name for the new alternating tile&quot;, // message
            &quot;Assets/Sprites &amp; Tiles/Tiles&quot;); // default folder in the project - change this to match your project!

    AssetDatabase.CreateAsset(ScriptableObject.CreateInstance&lt;AlternatingTile&gt;(), path);
}
#endif

Usage

If you followed the same menu item path as in the example above - in Unity Editor, click Assets &gt; Create &gt; 2D &gt; Tiles &gt; Alternating Tile

如何在Unity中创建棋盘格瓦片图案?

Select the asset in your Project window, and assign the two tiles you wish to alternate.

如何在Unity中创建棋盘格瓦片图案?

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).

如何在Unity中创建棋盘格瓦片图案?

huangapple
  • 本文由 发表于 2023年6月1日 09:42:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定