public RadioButtonBoxComponent( ComponentGroup componentGroup, Vector2 position, string headerLabel, string[] labels, int defaultValue ) : base(componentGroup, position, Vector2.zero) { _defaultValue = defaultValue; _activeIndex = defaultValue; _headerTextComponent = new TextComponent( componentGroup, position, new Vector2(BoxWidth, HeaderHeight), headerLabel, UiManager.NormalFontSize, alignment: TextAnchor.MiddleLeft ); position -= new Vector2(0, HeaderHeight + HeaderButtonMargin); _checkboxes = new CheckboxComponent[labels.Length]; _textComponents = new TextComponent[labels.Length]; for (var i = 0; i < labels.Length; i++) { var label = labels[i]; var checkboxComponent = _checkboxes[i] = new CheckboxComponent( componentGroup, position + new Vector2(-BoxWidth / 2f + ButtonSize / 2f, 0f), new Vector2(ButtonSize, ButtonSize), i == defaultValue, TextureManager.RadioButtonBg, TextureManager.RadioButtonToggle, false ); var index = i; checkboxComponent.SetOnToggle(value => { if (value) { OnClicked(index); } }); _textComponents[i] = new TextComponent( componentGroup, position + new Vector2((ButtonSize + ButtonTextMargin) / 2f, 0f), new Vector2(BoxWidth - ButtonSize - ButtonTextMargin, ButtonSize), label, UiManager.NormalFontSize, alignment: TextAnchor.MiddleLeft ); position -= new Vector2(0, ButtonSize + 8f); } }
public RadioButtonBoxComponent( ComponentGroup componentGroup, Vector2 position, Vector2 size, string[] labels, int defaultValue ) : base(componentGroup, position, size) { _defaultValue = defaultValue; var tempGameObject = new GameObject(); tempGameObject.transform.SetParent(UiManager.UiGameObject.transform); tempGameObject.SetActive(true); _toggleGroup = tempGameObject.AddComponent <ToggleGroup>(); _toggleGroup.allowSwitchOff = false; _toggles = new Toggle[labels.Length]; _activeIndex = defaultValue; for (var i = 0; i < labels.Length; i++) { var label = labels[i]; new TextComponent( componentGroup, position, new Vector2(TextWidth, 30), label, FontManager.UIFontRegular, 18, alignment: TextAnchor.LowerLeft ); var checkboxComponent = new CheckboxComponent( componentGroup, position + new Vector2(90, 0), new Vector2(20, 20), i == defaultValue, TextureManager.RadioBackground, TextureManager.RadioFilled ); var toggle = checkboxComponent.ToggleComponent; toggle.group = _toggleGroup; _toggleGroup.RegisterToggle(toggle); _toggles[i] = toggle; var index = i; checkboxComponent.SetOnToggle(value => { if (value) { OnClicked(index); } }); position -= new Vector2(0, 40); } }