public DynamicListBox CreateDynamicListField(GameObject parent, string name, List <string> items, float width, float height, int left, int top, Color color, Color hoverColor, Color textColor, int?fontSize = null, UnityAction <string> onItemSelect = null) { var dynamicListBox = new DynamicListBox(); dynamicListBox.label = CreateButton(parent, name, width, height, left, top, Color.clear, Color.clear, textColor); dynamicListBox.label.buttonText.alignment = TextAnchor.LowerLeft; dynamicListBox.label.buttonText.fontSize = fontSize ?? 20; dynamicListBox.items = items; dynamicListBox.onRefresh = () => { try { foreach (var prevOption in dynamicListBox.itemButtons) { if (prevOption.Key == null) { return; } _context.RemoveButton(prevOption.Key); } dynamicListBox.itemButtons = new Dictionary <UIDynamicButton, string>(); var itemTop = top + (height); foreach (var item in dynamicListBox.items) { var optionButton = CreateButton(parent, item, width, height, left, (int)itemTop, color + new Color(0.1f, 0.1f, 0.1f), hoverColor + new Color(0.1f, 0.1f, 0.1f), textColor + new Color(0.1f, 0.1f, 0.1f)); optionButton.buttonText.fontSize = dynamicListBox.label.buttonText.fontSize; optionButton.button.onClick.AddListener(() => { try { onItemSelect.Invoke(item); } catch (Exception e) { SuperController.LogError(e.ToString()); } }); dynamicListBox.itemButtons.Add(optionButton, item); itemTop += height; } } catch (Exception e) { SuperController.LogError(e.ToString()); } }; return(dynamicListBox); }
public static void SetItems(this DynamicListBox listBox, List <string> items) { listBox.items = items; listBox.onRefresh.Invoke(); }
public static void Refresh(this DynamicListBox listBox) { listBox.onRefresh.Invoke(); }