tilemap coloring learning script sample....
using UnityEngine;
using UnityEngine.Tilemaps;
public class TileMapColorShakerByVanden : MonoBehaviour
{
private enum ColorShakerType : int
{
SolidColor = 1,
SpectrumCycling1 = 2
}
[SerializeField]
[Header("Basic input")]
[Tooltip("Tilemap to change colors of")]
private Tilemap _tilemap = default;
[SerializeField]
[Tooltip("preferred Color")]
private Color _baseColor = default;
[Header("Type specifics")]
[SerializeField]
[Tooltip("The method used to change the color")]
private ColorShakerType _colorShakerType = default;
[SerializeField]
[Tooltip("Speed of color change according the chosen method")]
[Range(0.01f, 1.0f)]
private float _speed = default;
[SerializeField]
[Tooltip("Color change refresh rate per second ; 50 = smooth ; 1=rough,slow")]
[Range(1f, 50f)]
private float _refreshRate = 50f;
Color _cyclingColor;
float _r,_g,_b = 0.5f;
float _a = 1.0f;
float _prevR, _prevG, _prevB = 0.5f;
float _deltaR, _deltaG, _deltaB = 0.5f;
float[] _colorMatrix;
int _offset = 0;
bool _isApplyColor = false;
float _frames = 0f;
List<TileBase>[,] _tileArray;
/*
* https://docs.unity3d.com/ScriptReference/Color.html
* Color : This structure is used throughout Unity to pass colors around. Each color component is a floating point
* value with a range from 0 to 1.
*/
private void Awake()
{
_refreshRate = 50f - _refreshRate; // opposite : 0=fast, 50=slow,rough
_tilemap.CompressBounds(); // cleaned up tilemap without invisble outside deleted but remaining null tiles
_bounds = _tilemap.cellBounds; // tilemap coordinate e.g. -20 ... +20, -10 ... +10
if (_colorShakerType == ColorShakerType.SpectrumCycling1)
// each following _colorMatrix line for a R, G, B fade indicator of 0f or 1f ; if 0f then fade it to become darker ; if 1f to become brighter
_colorMatrix = new float[] {
0f,0f,1f,
0f,1f,0f,
0f,1f,1f,
1f,0f,0f,
1f,0f,1f,
1f,1f,0f,
1f,1f,1f }; // ... modulo of 3 required
_r = Mathf.Max(0.1f,Mathf.Min(_baseColor.r,0.9f));
_g = Mathf.Max(0.1f,Mathf.Min(_baseColor.g,0.9f));
_b = Mathf.Max(0.1f,Mathf.Min(_baseColor.b,0.9f));
_a = Mathf.Max(_baseColor.a,0.5f);
_cyclingColor = new Color(_r, _g, _b, _a);
_prevR = _r;
_prevG = _g;
_prevB = _b;
}
private void Start()
{
//_offsetX = System.Math.Abs(_bounds.xMin); // offset to be added to each local x position to ensure an x value between 0.._bounds.size.x
//_offsetY = System.Math.Abs(_bounds.yMin); // offset to be added to each local y position to ensure an y value between 0.._bounds.size.y
//_tileArray = new List<TileBase>[_bounds.size.x, _bounds.size.y];
//int x, y;
if (_colorShakerType == ColorShakerType.SolidColor)
_tilemap.color = _cyclingColor; // tint the complete tilemap ; all at once ; not each individual tile
if (_colorShakerType == ColorShakerType.SpectrumCycling1)
foreach (var pos in _bounds.allPositionsWithin)
{
//x = pos.x + _offsetX;
//y = pos.y + _offsetY;
if (_tilemap.HasTile(pos))
{
//_tileArray[x,y]=_tilemap.GetTile(pos); // put TileBase obj. into array
_tilemap.SetTileFlags(pos, TileFlags.None);
_tilemap.SetColor(pos, _cyclingColor); // open for more individual coloring... feel free to change :-)
}
}
}
private void FixedUpdate()
{
// 50 times per second by default
if (_colorShakerType == ColorShakerType.SpectrumCycling1)
{
if (_colorMatrix[_offset + 0] == 1f && _r < 0.95f)
_r += _speed * Time.fixedDeltaTime;
if (_colorMatrix[_offset + 0] == 0f && _r > 0.05f)
_r -= _speed * Time.fixedDeltaTime;
if (_colorMatrix[_offset + 1] == 1f && _g < 0.95f)
_g += _speed * Time.fixedDeltaTime;
if (_colorMatrix[_offset + 1] == 0f && _g > 0.05f)
_g -= _speed * Time.fixedDeltaTime;
if (_colorMatrix[_offset + 2] == 1f && _b < 0.95f)
_b += _speed * Time.fixedDeltaTime;
if (_colorMatrix[_offset + 2] == 0f && _b > 0.05f)
_b -= _speed * Time.fixedDeltaTime;
_deltaR = _r - _prevR;
_deltaG = _g - _prevG;
_deltaB = _b - _prevB;
if (_deltaR == 0f && _deltaG == 0f && _deltaB == 0f)
{
_offset += 3;
if (_offset >= _colorMatrix.Length - 1)
_offset = 0;
}
}
_cyclingColor = new Color(_r,_g,_b,_a);
_frames++;
if (_frames > _refreshRate)
{
_frames = 0;
_isApplyColor = true;
//Debug.Log($"_offset={_offset}, _r={_r}, _g={_g}, _b={_b}, _deltaR={_deltaR}, _deltaG={_deltaG}, _deltaB={_deltaB}");
}
_prevR = _r;
_prevG = _g;
_prevB = _b;
}
private void Update()
{
if (_isApplyColor)
{
if (_colorShakerType == ColorShakerType.SpectrumCycling1)
foreach (var pos in _bounds.allPositionsWithin)
if (_tilemap.HasTile(pos))
_tilemap.SetColor(pos, _cyclingColor);
_isApplyColor = false;
}
}
}