using System.Linq; using UnityEngine; using UnityEngine.Tilemaps; namespace UnityEditor.Tilemaps { /// /// Advanced tint brush for interpolated tint color per-cell. Requires the use of custom shader (see TintedTilemap.shader) and helper component TileTextureGenerator. /// [CustomGridBrush(false, false, false, "Tint Brush (Smooth)")] public class TintBrushSmooth : GridBrushBase { /// /// Factor to blend the Color of Tile with its color and this Brush's color /// public float m_Blend = 1f; /// /// Color of the Tile to tint /// public Color m_Color = Color.white; /// /// Tints tiles into a given position within the selected layers. /// The TintBrushSmooth overrides this to set the color of the Grid position 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) { TintTextureGenerator generator = GetGenerator(grid); if (generator != null) { var oldColor = generator.GetColor(grid as Grid, position); var blendColor = oldColor * (1 - m_Blend) + m_Color * m_Blend; generator.SetColor(grid as Grid, position, blendColor); } } /// /// Resets the color of the tiles in a given position within the selected layers to White. /// The TintBrushSmooth overrides this to set the color of the Grid position 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) { TintTextureGenerator generator = GetGenerator(grid); if (generator != null) { generator.SetColor(grid as Grid, position, Color.white); } } /// /// Picks the tint color given the coordinates of the cells. /// The TintBrushSmoot overrides this to provide color picking functionality. /// /// Grid to pick data from. /// Target of the picking operation. By default the currently selected GameObject. /// The coordinates of the cells to paint data from. /// Pivot of the picking brush. public override void Pick(GridLayout grid, GameObject brushTarget, BoundsInt position, Vector3Int pivot) { TintTextureGenerator generator = GetGenerator(grid); if (generator != null) { m_Color = generator.GetColor(grid as Grid, position.min); } } private TintTextureGenerator GetGenerator(GridLayout grid) { TintTextureGenerator generator = FindObjectOfType(); if (generator == null) { if (grid != null) { generator = grid.gameObject.AddComponent(); } } return generator; } } /// /// The Brush Editor for a Tint Brush Smooth. /// [CustomEditor(typeof(TintBrushSmooth))] public class TintBrushSmoothEditor : GridBrushEditorBase { /// /// The TintBrushSmooth for this Editor /// public TintBrushSmooth brush { get { return target as TintBrushSmooth; } } /// Returns all valid targets that the brush can edit. /// Valid targets for the TintBrushSmooth are any GameObjects with a Tilemap component. public override GameObject[] validTargets { get { return GameObject.FindObjectsOfType().Select(x => x.gameObject).ToArray(); } } /// /// Callback for painting the inspector GUI for the TintBrushSmooth in the Tile Palette. /// The TintBrushSmooth Editor overrides this to have a custom inspector for this Brush. /// public override void OnPaintInspectorGUI() { brush.m_Color = EditorGUILayout.ColorField("Color", brush.m_Color); brush.m_Blend = EditorGUILayout.Slider("Blend", brush.m_Blend, 0f, 1f); GUILayout.Label("Note: Tilemap needs to use TintedTilemap.shader!"); } } }