The RoadTile
example provides the ability to easily layout linear segments onto the TilemapA GameObject that allows you to quickly create 2D levels using tiles and a grid overlay. More info
See in Glossary, such as roads or pipes, with a minimal set of spritesA 2D graphic objects. If you are used to working in 3D, Sprites are essentially just standard textures but there are special techniques for combining and managing sprite textures for efficiency and convenience during development. More info
See in Glossary. The following is a script used to create the Tile.
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class RoadTile : Tile
{
public Sprite[] m_Sprites;
public Sprite m_Preview;
// This refreshes itself and other RoadTiles that are orthogonally and diagonally adjacent
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);
}
}
// This determines which sprite is used based on the RoadTiles that are adjacent to it and rotates it to fit the other tiles.
// As the rotation is determined by the RoadTile, the TileFlags.OverrideTransform is set for the 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");
}
}
// This determines if the Tile at the position is the same RoadTile.
private bool HasRoadTile(ITilemap tilemap, Vector3Int position)
{
return tilemap.GetTile(position) == this;
}
// The following determines which sprite to use based on the number of adjacent RoadTiles
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;
}
// The following determines which rotation to use based on the positions of adjacent RoadTiles
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
// The following is a helper that adds a menu item to create a RoadTile Asset
[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
}
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.