RoadTile
の例では、タイルマップに線形のセグメント(道路やパイプなど)を最小数のスプライトで簡単にレイアウトすることができます。タイルの作成に使用されるスクリプトは以下の通りです。
using UnityEngine;
using System.Collections;
# if UNITY_EDITOR
using UnityEditor;
# endif
public class RoadTile : Tile
{
public Sprite[] m_Sprites;
public Sprite m_Preview;
// そのTile 自体と、直角に、または斜めに接するその他の RoadTiles をリフレッシュします
public override void RefreshTile(Vector3Int location, ITilemap tilemap)
{
for (int yd = -1; yd <= 1; yd++)
for (int xd = -1; xd <= 1; xd++)
{
Vector3Int position = new Vector3Int(location.x + xd, location.y + yd, location.z);
if (HasRoadTile(tilemap, position))
tilemap.RefreshTile(position);
}
}
// 隣接するRoadTilesに基づいてどのスプライトが使用されているかを決定し、それを他のタイルに合わせて回転させます。
// RoadTile によって回転が決定されるため、TileFlags.OverrideTransform が tile に設定されます
public override void GetTileData(Vector3Int location, ITilemap tilemap, ref TileData tileData)
{
int mask = HasRoadTile(tilemap, location + new Vector3Int(0, 1, 0)) ? 1 : 0;
mask += HasRoadTile(tilemap, location + new Vector3Int(1, 0, 0)) ? 2 : 0;
mask += HasRoadTile(tilemap, location + new Vector3Int(0, -1, 0)) ? 4 : 0;
mask += HasRoadTile(tilemap, location + new Vector3Int(-1, 0, 0)) ? 8 : 0;
int index = GetIndex((byte)mask);
if (index >= 0 && index < m_Sprites.Length)
{
tileData.sprite = m_Sprites[index];
tileData.color = Color.white;
var m = tileData.transform;
m.SetTRS(Vector3.zero, GetRotation((byte) mask), Vector3.one);
tileData.transform = m;
tileData.flags = TileFlags.LockTransform;
tileData.colliderType = ColliderType.None;
}
else
{
Debug.LogWarning("Not enough sprites in RoadTile instance");
}
}
// その位置のTile は同じ RoadTile かを判断します
private bool HasRoadTile(ITilemap tilemap, Vector3Int position)
{
return tilemap.GetTile(position) == this;
}
// 接する RoadTile の数に基づいて、使用するスプライトを決定します
private int GetIndex(byte mask)
{
switch (mask)
{
case 0: return 0;
case 3:
case 6:
case 9:
case 12: return 1;
case 1:
case 2:
case 4:
case 5:
case 10:
case 8: return 2;
case 7:
case 11:
case 13:
case 14: return 3;
case 15: return 4;
}
return -1;
}
// 接する RoadTile の位置に基づいて、使用する回転角度を決定します
private Quaternion GetRotation(byte mask)
{
switch (mask)
{
case 9:
case 10:
case 7:
case 2:
case 8:
return Quaternion.Euler(0f, 0f, -90f);
case 3:
case 14:
return Quaternion.Euler(0f, 0f, -180f);
case 6:
case 13:
return Quaternion.Euler(0f, 0f, -270f);
}
return Quaternion.Euler(0f, 0f, 0f);
}
# if UNITY_EDITOR
// 以下はメニュー項目を加えて RoadTile アセットを作るヘルパーです
[MenuItem("Assets/Create/RoadTile")]
public static void CreateRoadTile()
{
string path = EditorUtility.SaveFilePanelInProject("Save Road Tile", "New Road Tile", "Asset", "Save Road Tile", "Assets");
if (path == "")
return;
AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<RoadTile>(), path);
}
# endif
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.