void createMapLoadButton(GameObject mapLoadContentGO, AvailableMap map, MapLoader loader) { Transform panelPrefab = getTransformByNameAndRoot("Panel", SuperController.singleton.mainMenuUI); Transform buttonPrefab = getTransformByNameAndRoot("Quit Button", SuperController.singleton.mainMenuUI); Transform textPrefab = getTransformByNameAndRoot("Text", SuperController.singleton.mainMenuUI); RectTransform buttonPrefabRT = buttonPrefab.GetComponent <RectTransform>(); RectTransform textPrefabRT = textPrefab.GetComponent <RectTransform>(); Image imagePrefab = buttonPrefab.GetComponent <Image>(); string style = buttonPrefab.GetComponent <UIStyleButton>().styleName; RectTransform mapLoadContentRT = mapLoadContentGO.GetComponent <RectTransform>(); GameObject mapLoadButtonGO = new GameObject("mapLoadButtonGO"); mapLoadButtonGO.transform.localScale = panelPrefab.localScale; mapLoadButtonGO.transform.SetParent(mapLoadContentGO.transform, false); Button mapLoadButtonUI = mapLoadButtonGO.AddComponent <Button>(); Image mapLoadButtonImage = mapLoadButtonGO.AddComponent <Image>(); UIStyleButton mplbStyle = mapLoadButtonGO.AddComponent <UIStyleButton>(); UIStyleImage mpliStyle = mapLoadButtonGO.AddComponent <UIStyleImage>(); RectTransform mapButtonRT = mapLoadButtonGO.GetComponent <RectTransform>(); //Copy values from existing components. CopyImageValues(imagePrefab, mapLoadButtonImage); mplbStyle.styleName = style; mpliStyle.styleName = style; CopyRectTransformValues(buttonPrefabRT, mapButtonRT); //Create new gameobject and ui for button text GameObject mapLoadTextGO = new GameObject("MapLoadText"); mapLoadTextGO.transform.localScale = textPrefab.localScale; mapLoadTextGO.transform.SetParent(mapLoadButtonGO.transform, false); Text mapLoadText = mapLoadTextGO.AddComponent <Text>(); UIStyleText mpltStyle = mapLoadButtonGO.AddComponent <UIStyleText>(); RectTransform mapTextRT = mapLoadTextGO.GetComponent <RectTransform>(); //Copy values from existing components. CopyRectTransformValues(textPrefabRT, mapTextRT); CopyTextValues(textPrefab.GetComponent <Text>(), mapLoadText); mpltStyle.styleName = style; mapLoadContentRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, mapLoadContentRT.rect.height + mapButtonRT.rect.height); //Update new components with adjusted values. mapLoadText.text = loader.Mapkey() + " - " + map.displayName; mapLoadButtonUI.onClick.AddListener(() => { LoadMap(map, loader); }); }
Button createMenuButton(string text) { //Get existing UI components. Transform panelPrefab = getTransformByNameAndRoot("Panel", SuperController.singleton.mainMenuUI); Transform buttonPrefab = getTransformByNameAndRoot("Quit Button", SuperController.singleton.mainMenuUI); Transform textPrefab = getTransformByNameAndRoot("Text", SuperController.singleton.mainMenuUI); Image imagePrefab = buttonPrefab.GetComponent <Image>(); RectTransform buttonPrefabRT = buttonPrefab.GetComponent <RectTransform>(); string style = buttonPrefab.GetComponent <UIStyleButton>().styleName; RectTransform textPrefabRT = textPrefab.GetComponent <RectTransform>(); //Create new gameobjects and associated UI components. GameObject mapLoadButtonGO = new GameObject("MapLoadButton"); mapLoadButtonGO.transform.localScale = buttonPrefab.localScale; mapLoadButtonGO.transform.SetParent(panelPrefab, false); Button mapLoadButtonUI = mapLoadButtonGO.AddComponent <Button>(); Image mapLoadButtonImage = mapLoadButtonGO.AddComponent <Image>(); UIStyleButton mplbStyle = mapLoadButtonGO.AddComponent <UIStyleButton>(); UIStyleImage mpliStyle = mapLoadButtonGO.AddComponent <UIStyleImage>(); RectTransform mapButtonRT = mapLoadButtonGO.GetComponent <RectTransform>(); //Copy values from existing components. CopyImageValues(imagePrefab, mapLoadButtonImage); mplbStyle.styleName = style; mpliStyle.styleName = style; CopyRectTransformValues(buttonPrefabRT, mapButtonRT); //Create new gameobject and ui for button text GameObject mapLoadTextGO = new GameObject("MapLoadText"); mapLoadTextGO.transform.localScale = textPrefab.localScale; mapLoadTextGO.transform.SetParent(mapLoadButtonGO.transform, false); Text mapLoadText = mapLoadTextGO.AddComponent <Text>(); UIStyleText mpltStyle = mapLoadButtonGO.AddComponent <UIStyleText>(); RectTransform mapTextRT = mapLoadTextGO.GetComponent <RectTransform>(); //Copy values from existing components. CopyRectTransformValues(textPrefabRT, mapTextRT); CopyTextValues(textPrefab.GetComponent <Text>(), mapLoadText); mpltStyle.styleName = style; //Update new components with adjusted values. mapLoadText.text = text; return(mapLoadButtonUI); }