using System.Linq;
using UnityEngine;
using UnityEngine.Tilemaps;
namespace UnityEditor.Tilemaps
{
///
/// This Brush changes the color of Tiles placed on a Tilemap.
///
[CustomGridBrush(true, false, false, "Tint Brush")]
public class TintBrush : GridBrushBase
{
///
/// Color of the Tile to tint
///
public Color m_Color = Color.white;
///
/// Tints tiles into a given position within the selected layers.
/// The TintBrush overrides this to set the color of the Tile to tint it.
///
/// Grid used for layout.
/// Target of the paint operation. By default the currently selected GameObject.
/// The coordinates of the cell to paint data to.
public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int position)
{
Tilemap tilemap = brushTarget.GetComponent();
if (tilemap != null)
{
SetColor(tilemap, position, m_Color);
}
}
///
/// Resets the color of the tiles in a given position within the selected layers to White.
/// The TintBrush overrides this to set the color of the Tile to White.
///
/// Grid used for layout.
/// Target of the erase operation. By default the currently selected GameObject.
/// The coordinates of the cell to erase data from.
public override void Erase(GridLayout grid, GameObject brushTarget, Vector3Int position)
{
Tilemap tilemap = brushTarget.GetComponent();
if (tilemap != null)
{
SetColor(tilemap, position, Color.white);
}
}
private static void SetColor(Tilemap tilemap, Vector3Int position, Color color)
{
TileBase tile = tilemap.GetTile(position);
if (tile != null)
{
if ((tilemap.GetTileFlags(position) & TileFlags.LockColor) != 0)
{
if (tile is Tile)
{
Debug.LogWarning("Tint brush cancelled, because Tile (" + tile.name + ") has TileFlags.LockColor set. Unlock it from the Tile asset debug inspector.");
}
else
{
Debug.LogWarning("Tint brush cancelled. because Tile (" + tile.name + ") has TileFlags.LockColor set. Unset it in GetTileData().");
}
}
tilemap.SetColor(position, color);
}
}
}
///
/// The Brush Editor for a Tint Brush.
///
[CustomEditor(typeof(TintBrush))]
public class TintBrushEditor : GridBrushEditorBase
{
/// Returns all valid targets that the brush can edit.
/// Valid targets for the TintBrush are any GameObjects with a Tilemap component.
public override GameObject[] validTargets
{
get
{
return GameObject.FindObjectsOfType().Select(x => x.gameObject).ToArray();
}
}
}
}