Skip to content

Instantly share code, notes, and snippets.

@Dssdiego
Created March 4, 2020 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dssdiego/858fb2b0ed27a25cb43f4d78ca64530c to your computer and use it in GitHub Desktop.
Save Dssdiego/858fb2b0ed27a25cb43f4d78ca64530c to your computer and use it in GitHub Desktop.
Unity Color Changer
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Image))]
public class ColorChanger : MonoBehaviour
{
private Image _img;
private List<Color> _colors = Constants.Colors;
private int _minIndex;
private int _maxIndex;
private int _currentIndex;
private Color _currentColor;
private void Start()
{
_img = GetComponent<Image>();
FillIndexes();
SetColor();
}
public void PreviousColor()
{
Debug.Log($"Current Index: {_currentIndex}");
Debug.Log($"Min Index: {_minIndex}");
if (_currentIndex == _minIndex)
{
_currentIndex = _maxIndex + 1;
}
_currentIndex--;
SetColor();
}
public void NextColor()
{
if (_currentIndex == _maxIndex)
{
_currentIndex = _minIndex - 1;
}
_currentIndex++;
SetColor();
}
private void FillIndexes()
{
_minIndex = 0;
_maxIndex = _colors.Count - 1;
_currentIndex = _minIndex;
Debug.Log($"Max Index: {_maxIndex}");
}
private void SetColor()
{
_img.color = _colors[_currentIndex];
_currentColor = _img.color;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment