public static void StretchDropObject(DropObject _dropobject) { TextGenerator generator = new TextGenerator(); Transform captionText = _dropobject.transform.Find("text"); UnityEngine.UI.Text textComponent = captionText.GetComponent <UnityEngine.UI.Text>(); var settings = textComponent.GetGenerationSettings(Vector2.zero); settings.generateOutOfBounds = true; float offsetWidth = textComponent.fontSize / 2 + Mathf.Abs(textComponent.GetComponent <RectTransform>().sizeDelta.x); float width = generator.GetPreferredWidth(textComponent.text, settings) / textComponent.pixelsPerUnit + offsetWidth; RectTransform rt = _dropobject.GetComponent <RectTransform>(); rt.sizeDelta = new Vector2(width, rt.sizeDelta.y); }
private static void decorateElements(GameObject _expression, List <BlockModel.Element> _elements, Dictionary <string, string> _variants, CustomElementModel.CustomElementStatus _elementStatus) { RectTransform space = _expression.transform.Find("__space__").GetComponent <RectTransform>(); space.gameObject.SetActive(_elements.Count == 0); foreach (BlockModel.Element element in _elements) { GameObject clone = null; if (element.type.Equals("text")) { clone = GameObject.Instantiate(templateElementText.gameObject); UnityEngine.UI.Text text = clone.GetComponent <UnityEngine.UI.Text>(); text.text = element.value; FacadeUtility.StretchText(text); } else if (element.type.Equals("input")) { clone = GameObject.Instantiate(templateElementInput.gameObject); InputField input = clone.GetComponent <InputField>(); input.text = element.value; if (_variants.ContainsKey(element.value)) { input.text = _variants[element.value]; } FacadeUtility.StretchInput(input); input.onEndEdit.AddListener((_text) => { if (null != OnInputUpdated) { OnInputUpdated(clone.transform.parent.name, element.value, _text); } }); } else if (element.type.Equals("dropdown")) { clone = GameObject.Instantiate(templateElementDropdown.gameObject); Dropdown dropdown = clone.GetComponent <Dropdown>(); FacadeUtility.StretchDropdown(dropdown); dropdown.onValueChanged.AddListener((_value) => { if (null != OnDropdownUpdated) { OnDropdownUpdated(clone.transform.parent.name, element.value, dropdown.value); } }); } else if (element.type.Equals("object")) { clone = GameObject.Instantiate(templateElementObject.gameObject); DropObject drop = clone.AddComponent <DropObject>(); FacadeUtility.StretchDropObject(drop); if (_variants.ContainsKey(element.value)) { string uuid = _variants[element.value]; if (!string.IsNullOrEmpty(uuid)) { if (objectElements.ContainsKey(uuid)) { ObjectElement oe = objectElements[uuid]; drop.captionText.text = oe.alias; drop.imgIcon.gameObject.SetActive(true); drop.imgIcon.sprite = oe.icon; } } } drop.onObjectDrop = (_dragObject) => { if (null != OnDropObjectUpdated) { OnDropObjectUpdated(clone.transform.parent.name, element.value, _dragObject.name); } }; } else { clone = buildCustomElement(element.type, element.value, _variants, _elementStatus); } if (null == clone) { continue; } clone.name = element.value; clone.transform.SetParent(_expression.transform); clone.transform.localScale = Vector3.one; } }