/// <summary> /// creates the menu item as a visible object in the 3D scene /// </summary> /// <param name="parentMenu">The menu to which the item belongs</param> /// <param name="parent">The parent menu item</param> public void Create(Menu parentMenu, CustomMenuItem parent) { this.parentMenuItem = parent; this.parentMenu = parentMenu; subMenuOpened = false; if (menuStyle == null) { menuStyle = parentMenu.defaultMenuStyle; } containerInstance = GameObject.Instantiate(menuStyle, parentMenu.transform); menuStyleAdapter = containerInstance.GetComponent <MenuStyleAdapter>(); menuStyleAdapter.Initialize(); menuStyleAdapter.RegisterForClickEvent(OnClick); menuStyleAdapter.UpdateText(text); menuStyleAdapter.UpdateIcon(icon); menuStyleAdapter.ItemEnabled = ItemEnabled; if (markOnClick) { menuStyleAdapter.Marked = marked; } if (onClickEvent == null) { onClickEvent = new StringEvent(); } }
public void InstantiateMenu(Vector3 instantiatePosition, Vector3 parentItemSize, List <CustomMenuItem> menu, CustomMenuItem parent, bool isSubMenu, Direction alignment) { // invoke the external initialization if it exists // this can e.g. disable certain buttons based on other states of the application if (externalInitialization != null) { externalInitialization.Invoke(); } Vector3 origInstantiatePos = instantiatePosition; if (isSubMenu) { instantiatePosition.y -= parentItemSize.y + padding; } MenuStyleAdapter lastMenuStyleAdapter = null; // instantiate the menu for (int i = 0; i < menu.Count; i++) { if (menu[i].visibleTo == PlayerType.ALL || menu[i].visibleTo == InformationManager.Instance.playerType) { menu[i].Create(this, parent); if (i > 0) { // set the correct position if (alignment == Direction.HORIZONTAL) { // if the previous item was created => move to the next position to place the current item there if (lastMenuStyleAdapter != null) { // get to the middle between the previous and the current item instantiatePosition.x += (lastMenuStyleAdapter.Size.x + padding) / 2; // get to the center of the current item instantiatePosition.x += (menu[i].MenuSytleAdapter.Size.x + padding) / 2; } } else if (alignment == Direction.VERTICAL) { // if the previous item was created => move to the next position to place the current item there if (lastMenuStyleAdapter != null) { // get to the middle between the previous and the current item instantiatePosition.y -= (lastMenuStyleAdapter.Size.y + padding) / 2; // get to the center of the current item instantiatePosition.y -= (menu[i].MenuSytleAdapter.Size.y + padding) / 2; } } else if (alignment == Direction.GRID) { // if the previous item was created => move to the next position to place the current item there if (lastMenuStyleAdapter != null) { // get to the middle between the previous and the current item instantiatePosition.x += (lastMenuStyleAdapter.Size.x + padding) / 2; // get to the center of the current item instantiatePosition.x += (menu[i].MenuSytleAdapter.Size.x + padding) / 2; // if one line is filled => move to next line if (i % itemsInOneLine == 0) { // get to the middle between the previous and the current item if (lastMenuStyleAdapter != null) { instantiatePosition.y -= (lastMenuStyleAdapter.Size.y + padding) / 2; } // get to the center of the current item instantiatePosition.y -= (menu[i].MenuSytleAdapter.Size.y + padding) / 2; // also reset the x coordinate instantiatePosition.x = origInstantiatePos.x; } } } } menu[i].Position = instantiatePosition; lastMenuStyleAdapter = menu[i].MenuSytleAdapter; } } }