public comboButtonPressed ( GameObject button ) : void | ||
button | GameObject | |
return | void |
public static void AddButton(ComboBox box, string text) { //Debug.Log("Adding button..."); GameObject panel = box.transform.FindChild("Panel").gameObject; List<GameObject> children = new List<GameObject>(box.transform.FindChild("Panel").childCount); for (int i = 0; i < box.transform.FindChild("Panel").childCount; i++) children.Add(panel.transform.GetChild(i).gameObject); if (children.Exists(child => child.transform.FindChild("Text").GetComponent<Text>().text == text)) return; GameObject newButton = GameObject.Instantiate(box.transform.FindChild("ComboButton").gameObject) as GameObject; GameObject parent = box.transform.parent.gameObject; float canvasScaleFactor = box.RootCanvas.GetComponent<Canvas>().scaleFactor; newButton.transform.SetParent(box.gameObject.transform.FindChild("Panel")); newButton.GetComponent<Button>().onClick.RemoveAllListeners(); newButton.GetComponent<Button>().onClick.AddListener(() => box.comboButtonPressed(newButton)); newButton.transform.FindChild("Text").GetComponent<Text>().text = text; RectTransform buttonTransform = newButton.GetComponent<RectTransform>(); RectTransform panelTransform = box.gameObject.transform.FindChild("Panel").gameObject.GetComponent<RectTransform>(); Rect buttonRect = buttonTransform.rect; Rect panelRect = panelTransform.rect; //float distance = box.ButtonDistance / canvasScaleFactor; //Debug.Log(distance); float offset = buttonRect.height * (box.buttons.Count); //+ distance; Vector2 panelPos = panelTransform.anchoredPosition; //Debug.Log(panelPos); Vector2 buttonPos = new Vector2(panelTransform.anchoredPosition.x, panelTransform.anchoredPosition.y - offset); //Debug.Log (buttonPos); buttonTransform.anchoredPosition = buttonPos; float newHeigth = buttonRect.height * (box.buttons.Count + 1.0f);// + distance; panelTransform.sizeDelta = new Vector2(panelRect.width, newHeigth); newButton.transform.localScale = newButton.transform.localScale * canvasScaleFactor; box.buttons.Add(newButton); if (box.buttons.Count == 1) box.transform.FindChild("ComboButton").FindChild("Text").gameObject.GetComponent<Text>().text = text; }