private void CreateMenuItems(List <MenuItemUIModel> models, HorizontalMenuSelectUI instance) { // Create menu items instance.container.DestroyChildren(); instance.menuItems = new List <MenuItemUI>(); int i = 0; foreach (MenuItemUIModel model in models) { MenuItemUI menuItem = MenuItemUI.Initialize(instance.menuItemPrefab, instance.container, model); menuItem.RectTransform.anchoredPosition = new Vector2(i * menuItem.RectTransform.sizeDelta.x, 0); if (i == focus) { menuItem.Highlight(); } else { menuItem.UnHighlight(); } instance.menuItems.Add(menuItem); i++; } }
public static PageUI Initialize(GameObject parent, List <MenuItemUIModel> models, MenuItemUI menuItemPrefab, int pageIdx, int?selected = null) { PageUI instance = new GameObject($"Page {pageIdx}").AddComponent <PageUI>(); instance.gameObject.transform.SetParent(parent.transform); RectTransform rectTransform = instance.gameObject.AddComponent <RectTransform>(); rectTransform.localScale = new Vector3(1, 1, 1); rectTransform.anchorMin = new Vector2(0, 0); rectTransform.anchorMax = new Vector2(1, 1); rectTransform.offsetMin = new Vector2(0, 0); rectTransform.offsetMax = new Vector2(0, 0); // Generate list items instance.MenuItems = new List <MenuItemUI>(); // Create elements foreach (MenuItemUIModel model in models) { instance.MenuItems.Add(MenuItemUI.Initialize( menuItemPrefab, instance.GetComponent <RectTransform>(), model )); } // Position elements int i = 0; foreach (MenuItemUI listItem in instance.MenuItems) { listItem.RectTransform.anchoredPosition = new Vector2 (0, i * -listItem.RectTransform.sizeDelta.y); i++; } // Highlight selected element instance.HighlightItem((selected.HasValue) ? selected.Value : 0); return(instance); }
public static MatrixMenuSelectUI Initialize( MatrixMenuSelectUI prefab, RectTransform parent, List <List <MenuItemUIModel> > models, bool cancelable = false ) { MatrixMenuSelectUI instance = Instantiate(prefab, parent).GetComponentWithError <MatrixMenuSelectUI>(); instance.menuItems = new List <List <MenuItemUI> >(); instance.cancelable = cancelable; // Create menu items instance.container.DestroyChildren(); int rowIdx = 0; foreach (List <MenuItemUIModel> row in models) { int colIdx = 0; instance.menuItems.Add(new List <MenuItemUI>()); foreach (MenuItemUIModel model in row) { MenuItemUI menuItem = MenuItemUI.Initialize(instance.menuItemPrefab, instance.container, model); menuItem.RectTransform.anchoredPosition = new Vector2( colIdx * menuItem.RectTransform.sizeDelta.x, rowIdx * -menuItem.RectTransform.sizeDelta.y ); instance.menuItems.Last().Add(menuItem); colIdx++; } rowIdx++; } return(instance); }