64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace UI
|
|
{
|
|
public class ColorPicker : MonoBehaviour
|
|
{
|
|
[SerializeField] private Renderer previewRenderer;
|
|
|
|
[SerializeField] private Button buttonNext;
|
|
[SerializeField] private Button buttonPrev;
|
|
[SerializeField] private Image previewImage;
|
|
|
|
private int _currentIndex = 0;
|
|
private Color _currentColor;
|
|
private Color[] _colors = new Color[] { Color.green, Color.magenta, Color.yellow, Color.cyan };
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
|
|
public Color Color => _currentColor;
|
|
|
|
void Start()
|
|
{
|
|
_currentColor = _colors[_currentIndex];
|
|
buttonNext.onClick.AddListener(NextColor);
|
|
buttonPrev.onClick.AddListener(PrevColor);
|
|
|
|
var newMaterial = new Material(previewRenderer.sharedMaterial);
|
|
newMaterial.color = _currentColor;
|
|
previewRenderer.sharedMaterial = newMaterial;
|
|
|
|
UpdateMaterialColor();
|
|
}
|
|
|
|
private void UpdateMaterialColor()
|
|
{
|
|
_currentColor = _colors[_currentIndex];
|
|
previewImage.color = _currentColor;
|
|
previewRenderer.sharedMaterial.color = _currentColor;
|
|
}
|
|
|
|
private void PrevColor()
|
|
{
|
|
_currentIndex--;
|
|
if (_currentIndex < 0)
|
|
{
|
|
_currentIndex = _colors.Length - 1;
|
|
}
|
|
|
|
UpdateMaterialColor();
|
|
}
|
|
|
|
private void NextColor()
|
|
{
|
|
_currentIndex++;
|
|
if (_currentIndex == _colors.Length)
|
|
{
|
|
_currentIndex = 0;
|
|
}
|
|
|
|
UpdateMaterialColor();
|
|
}
|
|
}
|
|
}
|