- Home /
How can I get the TileData needed to use the TileBase.GetTileData function?
I have created the following custom tile and slightly altered the "GetTileData" function, to become my "ChangeTile" function. It requires the same information to be passed over, but also requires a Sprite:
using UnityEngine;
using UnityEngine.Tilemaps;
[CreateAssetMenu(fileName = "New AStarTile", menuName = "Tiles/AStarTile")]
public class AStarTile : Tile
{
public TileScriptableObject tileScriptableObject;
public Sprite litSide;//Tile image that should be shown when lit
public Sprite darkSide;//Tile image that should be shown when dark
public bool currentlyLit = false;//Whether the tile has been flipped
public void ChangeTile(Vector3Int position, ITilemap tilemap, ref TileData tileData, Sprite newSprite)
{
tileData.sprite = newSprite;
tilemap.RefreshTile(position);
}
}
I am using the following line to call the function:
selectedTile.ChangeTile(clickPos, tileMap, , selectedTile.litSide);
I dont understand how I find "ref TileData tileData" needed for the function. Like what is the exact thing I put in the 3rd slot of the line above? I can provide the full script if you need more information. Thanks.
Answer by AnOrdinarySandwich · Feb 09, 2021 at 03:25 AM
Almost a year late, but if you take a look at the TileBase documentation (from which Tile is inherited), you'll find several methods to override called GetTileData and RefreshTile. Within these methods (gleaning usage details from the examples on each page), your code has access to all the information required to perform a change to what sprite is displayed. Of course, your code will have to provide what sprites are used and how to determine which is shown, but the examples are quite informative!
Your answer
Follow this Question
Related Questions
Tilemaps making feature 1 Answer
Can you add sprites to existing sprite sheet without breaking the tile pallet and in-game tiles 0 Answers
Spawning a specific Tile in a random position a certain distance from the player character 1 Answer
I can‘t assign a folder to my tile palette,I cannot assign a folder to my tile palette 0 Answers
Unity 2D TileMap Rule Tile Carpet 1 Answer