Created
March 4, 2020 01:20
-
-
Save Dssdiego/858fb2b0ed27a25cb43f4d78ca64530c to your computer and use it in GitHub Desktop.
Unity Color Changer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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