private void CreateMenuButton(object data) { if (this.root.menuItem.transform.childCount > 0) { for (int i = this.root.menuItem.transform.childCount - 1; i >= 0; i--) { UnityEngine.Object.DestroyObject(this.root.menuItem.transform.GetChild(i).gameObject); } } if ((this.itemMenuPrefab != null) && (this.root.menuItem != null)) { StyledItem item = UnityEngine.Object.Instantiate(this.itemMenuPrefab) as StyledItem; item.Populate(data); item.transform.SetParent(this.root.menuItem.transform, false); RectTransform component = item.GetComponent <RectTransform>(); component.pivot = new Vector2(0.5f, 0.5f); component.anchorMin = Vector2.zero; component.anchorMax = Vector2.one; component.offsetMin = Vector2.zero; component.offsetMax = Vector2.zero; this.root.gameObject.hideFlags = HideFlags.HideInHierarchy; Button button = item.GetButton(); if (button != null) { button.onClick.AddListener(new UnityAction(this.TogglePanelState)); } } }
private void CreateMenuButton(StyledItem toCreate) { if (_root.MenuItem.transform.childCount > 0) { for (var i = _root.MenuItem.transform.childCount - 1; i >= 0; --i) { DestroyObject(_root.MenuItem.transform.GetChild(i).gameObject); } } if (toCreate != null && _root.MenuItem != null) { var menuItem = Instantiate(toCreate); menuItem.transform.SetParent(_root.MenuItem.transform, false); var mt = menuItem.GetComponent <RectTransform>(); mt.pivot = new Vector2(0.5f, 0.5f); mt.anchorMin = Vector2.zero; mt.anchorMax = Vector2.one; mt.offsetMin = Vector2.zero; mt.offsetMax = Vector2.zero; _root.gameObject.hideFlags = HideFlags.HideInHierarchy; // should really be HideAndDontSave, but unity crashes var b = menuItem.GetButton(); if (b != null) { b.onClick.AddListener(TogglePanelState); } } }
public void OnItemClicked(StyledItem item, int index) { SelectedIndex = index; TogglePanelState(); // close OnSelectionChanged?.Invoke(item); }
public void InitControl() { if (root != null) { DestroyImmediate(root.gameObject); } if (containerPrefab != null) { // create RectTransform own = GetComponent <RectTransform>(); root = Instantiate(containerPrefab, own.position, own.rotation) as StyledComboBoxPrefab; root.transform.SetParent(this.transform, false); scrollControl = root.itemPanel.GetComponent <ScrollRect> (); scrollControl.scrollSensitivity = ScrollSensitivity; RectTransform rt = root.GetComponent <RectTransform>(); rt.pivot = new Vector2(0.5f, 0.5f); //root.anchoredPosition = Vector2.zero; rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.one; rt.offsetMax = Vector2.zero; rt.offsetMin = Vector2.zero; root.gameObject.hideFlags = HideFlags.HideInHierarchy; // should really be HideAndDontSave, but unity crashes root.itemPanel.alpha = 0.0f; // create menu item StyledItem toCreate = itemMenuPrefab; if (toCreate == null) { toCreate = itemPrefab; } CreateMenuButton(toCreate); } }
public void OnItemClicked(StyledItem item, int index) { SelectedIndex = index; TogglePanelState(); if (OnSelectionChanged != null) { OnSelectionChanged(item); } }
private void AddItem(object data) { if (itemPrefab != null) { Vector3[] corners = new Vector3[4]; itemPrefab.GetComponent <RectTransform>().GetLocalCorners(corners); Vector3 pos = corners[0]; float sizeY = pos.y - corners[2].y; pos.y = items.Count * sizeY - 5f; StyledItem styledItem = Instantiate(itemPrefab, pos, root.itemRoot.rotation) as StyledItem; RectTransform trans = styledItem.GetComponent <RectTransform>(); styledItem.Populate(data); trans.SetParent(root.itemRoot.transform, false); trans.pivot = new Vector2(0, 1); trans.anchorMin = new Vector2(0, 1); trans.anchorMax = Vector2.one; trans.anchoredPosition = new Vector2(0.0f, pos.y); items.Add(styledItem); trans.offsetMin = new Vector2(0, pos.y + sizeY); trans.offsetMax = new Vector2(0, pos.y); float offsetSize = (items.Count + 1) * sizeY; if (-offsetSize > root.GetComponent <RectTransform> ().rect.height) { scrollControl.vertical = true; scrollControl.verticalScrollbar.gameObject.SetActive(true); } root.itemRoot.offsetMin = new Vector2(root.itemRoot.offsetMin.x, offsetSize); Button b = styledItem.GetButton(); int curIndex = items.Count - 1; if (b != null) { b.onClick.AddListener(delegate() { OnItemClicked(styledItem, curIndex); }); } } }
private void CreateMenuButton(StyledItem toCreate) { if (root.menuItem.transform.childCount > 0) { for (int i = root.menuItem.transform.childCount - 1; i >= 0; --i) DestroyObject(root.menuItem.transform.GetChild(i).gameObject); } if (toCreate != null && root.menuItem != null) { StyledItem menuItem = Instantiate(toCreate) as StyledItem; menuItem.transform.parent = root.menuItem.transform; RectTransform mt = menuItem.GetComponent<RectTransform>(); mt.pivot = new Vector2(0.5f, 0.5f); mt.anchorMin = Vector2.zero; mt.anchorMax = Vector2.one; mt.offsetMin = Vector2.zero; mt.offsetMax = Vector2.zero; root.gameObject.hideFlags = HideFlags.HideInHierarchy; // should really be HideAndDontSave, but unity crashes Button b = menuItem.GetButton(); if (b != null) { b.onClick.AddListener(TogglePanelState); } } }
public void OnItemClicked(StyledItem item, int index) { SelectedIndex = index; TogglePanelState(); // close if (OnSelectionChanged != null) { OnSelectionChanged(item); } }