/// Credit David Gileadi /// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/pull-requests/12 using System; using UnityEngine.Events; using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions { // Segmented control, like a group of buttons [AddComponentMenu("UI/Extensions/Segmented Control")] [RequireComponent(typeof(RectTransform))] public class SegmentedControl : UIBehaviour { private Button[] m_segments; [SerializeField] [Tooltip("A GameObject with an Image to use as a separator between segments. Size of the RectTransform will determine the size of the separator used.\nNote, make sure to disable the separator GO so that it does not affect the scene")] private Graphic m_separator; private float m_separatorWidth = 0; [SerializeField] [Tooltip("When True, it allows each button to be toggled on/off")] private bool m_allowSwitchingOff = false; [SerializeField] [Tooltip("The selected default for the control (zero indexed array)")] private int m_selectedSegmentIndex = -1; // Event delegates triggered on click. [SerializeField] [Tooltip("Event to fire once the selection has been changed")] private SegmentSelectedEvent m_onValueChanged = new SegmentSelectedEvent(); protected internal Button selectedSegment; protected float SeparatorWidth { get { if (m_separatorWidth == 0 && separator) { m_separatorWidth = separator.rectTransform.rect.width; var image = separator.GetComponent(); if (image) m_separatorWidth /= image.pixelsPerUnit; } return m_separatorWidth; } } [Serializable] public class SegmentSelectedEvent : UnityEvent { } public Button[] segments { get { if (m_segments == null || m_segments.Length == 0) { m_segments = GetChildSegments(); } return m_segments; } } [SerializeField] public Color selectedColor; public Graphic separator { get { return m_separator; } set { m_separator = value; m_separatorWidth = 0; LayoutSegments(); } } public bool allowSwitchingOff { get { return m_allowSwitchingOff; } set { m_allowSwitchingOff = value; } } public int selectedSegmentIndex { get { return Array.IndexOf(segments, selectedSegment); } set { value = Math.Max(value, -1); value = Math.Min(value, segments.Length - 1); m_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; } } } public SegmentSelectedEvent onValueChanged { get { return m_onValueChanged; } set { m_onValueChanged = value; } } protected SegmentedControl() { } protected override void Start() { base.Start(); LayoutSegments(); if (m_selectedSegmentIndex != -1) selectedSegmentIndex = m_selectedSegmentIndex; } #if UNITY_EDITOR protected override void OnValidate() { base.OnValidate(); if (separator) LayoutSegments(); if (m_selectedSegmentIndex != -1) selectedSegmentIndex = m_selectedSegmentIndex; if (m_selectedSegmentIndex > transform.childCount) { selectedSegmentIndex = transform.childCount - 1; } if (selectedColor == new Color(0, 0, 0, 0)) { selectedColor = new Color(0f, 0.455f, 0.894f); } } #endif private Button[] GetChildSegments() { var buttons = GetComponentsInChildren