/// <summary> /// Creates a GUI system identical to radio buttons by using toggle buttons. /// Giving 0 or fewer buttons per row in the layout object puts all choices on the first row. /// </summary> public static int RadioButtons(GUIContent[] items, int currentSelected, RadioButtonsLayout layout, bool scaleToScreen, GUIStyle style = null) { if (scaleToScreen) { layout = new RadioButtonsLayout(layout); layout.TopLeftChoicePos = new Vector2(layout.TopLeftChoicePos.x * Screen.width, layout.TopLeftChoicePos.y * Screen.height); layout.ButtonSize = new Vector2(layout.ButtonSize.x * Screen.width, layout.ButtonSize.y * Screen.height); layout.ButtonSpacing = new Vector2(layout.ButtonSpacing.x * Screen.width, layout.ButtonSpacing.y * Screen.height); } Rect buttonRect = new Rect(layout.TopLeftChoicePos.x, layout.TopLeftChoicePos.y, layout.ButtonSize.x, layout.ButtonSize.y); bool tempToggled; for (int i = 0; i < items.Length; ++i) { //Show the button. if (style == null) { tempToggled = GUI.Toggle(buttonRect, (currentSelected == i), items[i]); } else { tempToggled = GUI.Toggle(buttonRect, (currentSelected == i), items[i], style); } //React to it being pressed. if (tempToggled) { currentSelected = i; } //Move the position counter for the next button. buttonRect.x += layout.ButtonSpacing.x + buttonRect.width; if (layout.ButtonsPerRow > 0 && ((i + 1) % layout.ButtonsPerRow == 0)) { buttonRect.x = layout.TopLeftChoicePos.x; buttonRect.y += layout.ButtonSize.y + layout.ButtonSpacing.y; } } return(currentSelected); }
public RadioButtonsLayout(RadioButtonsLayout copy) : this(copy.TopLeftChoicePos, copy.ButtonSize, copy.ButtonSpacing, copy.ButtonsPerRow) { }