using System; using System.Collections; using System.Collections.Generic; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.Serialization; namespace UnityEngine.UI.Extensions { // Segmented control, like a group of buttons [AddComponentMenu("UI/Extensions/Segmented Control")] [RequireComponent(typeof(RectTransform))] public class SegmentedControl : UIBehaviour { public Button[] segments { get { if (_segments == null || _segments.Length == 0) { _segments = GetChildSegments(); } return _segments; } } private Button[] _segments; [SerializeField] public Color selectedColor; [SerializeField] private Graphic _separator; public Graphic separator { get { return _separator; } set { _separator = value; _separatorWidth = 0; LayoutSegments(); } } private float _separatorWidth = 0; private float separatorWidth { get { if (_separatorWidth == 0 && separator) { _separatorWidth = separator.rectTransform.rect.width; var image = separator.GetComponent(); if (image) _separatorWidth /= image.pixelsPerUnit; } return _separatorWidth; } } [SerializeField] private bool _allowSwitchingOff = false; public bool allowSwitchingOff { get { return _allowSwitchingOff; } set { _allowSwitchingOff = value; } } protected internal Button selectedSegment; [SerializeField] private int _selectedSegmentIndex = -1; public int selectedSegmentIndex { get { return Array.IndexOf(segments, selectedSegment); } set { value = Math.Max(value, -1); value = Math.Min(value, segments.Length - 1); _selectedSegmentIndex = value; if (value == -1) { if (selectedSegment) { selectedSegment.GetComponent().selected = false; selectedSegment = null; } } else { #if UNITY_EDITOR segments[value].GetComponent().StoreTextColor(); #endif segments[value].GetComponent().selected = true; } } } [Serializable] public class SegmentSelectedEvent : UnityEvent { } // Event delegates triggered on click. [SerializeField] private SegmentSelectedEvent _onValueChanged = new SegmentSelectedEvent(); public SegmentSelectedEvent onValueChanged { get { return _onValueChanged; } set { _onValueChanged = value; } } protected SegmentedControl() { } protected override void Start() { base.Start(); LayoutSegments(); if (_selectedSegmentIndex != -1) selectedSegmentIndex = _selectedSegmentIndex; } #if UNITY_EDITOR protected override void OnValidate() { base.OnValidate(); if (separator) LayoutSegments(); if (_selectedSegmentIndex != -1) selectedSegmentIndex = _selectedSegmentIndex; } #endif private Button[] GetChildSegments() { var buttons = GetComponentsInChildren