| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | [RequireComponent(typeof(Button))] |
| | 6 | | public class ColorToggle : MonoBehaviour |
| | 7 | | { |
| | 8 | | [SerializeField] private Image colorPicker; |
| | 9 | | [SerializeField] private Image selectionHighlight; |
| | 10 | |
|
| | 11 | | private bool selectedValue; |
| | 12 | | private Button button; |
| | 13 | |
|
| 44 | 14 | | public Color Color { get; private set; } |
| | 15 | |
|
| | 16 | | public bool Selected |
| | 17 | | { |
| 0 | 18 | | get => selectedValue; |
| | 19 | |
|
| | 20 | | set |
| | 21 | | { |
| 22 | 22 | | selectedValue = value; |
| 22 | 23 | | selectionHighlight.enabled = selectedValue; |
| 22 | 24 | | } |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public event Action<ColorToggle> OnClicked; |
| | 28 | |
|
| | 29 | | private void Awake() |
| | 30 | | { |
| 0 | 31 | | button = GetComponent<Button>(); |
| 0 | 32 | | button.onClick.AddListener(() => OnClicked?.Invoke(this)); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | private void OnDestroy() |
| | 36 | | { |
| 0 | 37 | | button.onClick.RemoveAllListeners(); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | public void Initialize(Color c, bool on) |
| | 41 | | { |
| 22 | 42 | | Color = c; |
| 22 | 43 | | colorPicker.color = Color; |
| 22 | 44 | | Selected = on; |
| 22 | 45 | | } |
| | 46 | | } |