public InputFieldTextMeshPro(TextComp comp, UnityAction <string> editEndCallback) { this.linkedTextComp = comp; TMP_DefaultControls.Resources resources = new TMP_DefaultControls.Resources(); inputField = TMP_DefaultControls.CreateInputField(resources); inputField.transform.SetParent(comp.GetTransform(), false); RectTransform inputFieldRectTrans = inputField.transform.GetComponent <RectTransform>(); inputFieldRectTrans.anchorMin = Vector2.zero; inputFieldRectTrans.anchorMax = Vector2.one; inputFieldRectTrans.offsetMin = Vector2.zero; inputFieldRectTrans.offsetMax = Vector2.zero; tmpInputFieldComp = inputField.GetComponent <TMP_InputField>(); UnityAction <string> action = new UnityAction <string>(UpdateTextWhileTyping); tmpInputFieldComp.onValueChanged.AddListener(action); // Copy text into input field tmpInputFieldComp.text = comp.GetText(); tmpInputFieldComp.lineType = TMP_InputField.LineType.MultiLineNewline; // Disable background image inputField.GetComponent <Image>().enabled = false; // Create preset of text component property values TextMeshProUGUI textComp = inputField.transform.Find("Text Area").Find("Text").GetComponent <TextMeshProUGUI>(); TextMeshProUGUI placeholderTextComp = inputField.transform.Find("Text Area").Find("Placeholder").GetComponent <TextMeshProUGUI>(); string placeholderEmptyString = "Enter text..."; Preset textCompPreset = (comp as TextCompTextMeshProUGUI).GetPresetOfInternalComp(); bool success = textCompPreset.ApplyTo(textComp); success = textCompPreset.ApplyTo(placeholderTextComp); placeholderTextComp.text = placeholderEmptyString; placeholderTextComp.enabled = false; // Reset "Text Area" RectTransform component RectTransform textAreaRectTransform = inputField.transform.Find("Text Area").GetComponent <RectTransform>(); Preset rectTransformResetter = new Preset(inputField.transform.Find("Text Area").Find("Text").GetComponent <RectTransform>()); success = rectTransformResetter.ApplyTo(textAreaRectTransform); TMP_InputField inputFieldComp = inputField.GetComponent <TMP_InputField>(); inputFieldComp.onEndEdit.AddListener(editEndCallback); // @NOTE This fixes caret not spawned when creating TextMeshPro input field during runtime. tmpInputFieldComp.enabled = false; tmpInputFieldComp.enabled = true; }
private void SpawnInputField() { this.isInEditMode = true; RuntimeTextEdit.oneInstanceInEditMode = true; onEnterEditMode.Invoke(); inputField = textComp.CreateInputField(OnInputFieldEditExit); inputField.SetText(textComp.GetText()); if (textComp.CanBeEditedInPlace()) { textComp.Hide(); inputField.SetParent(textComp.GetTransform(), false); } else { GameObject canvas = GetCanvas(); inputField.SetParent(canvas.transform, false); } textComp.OnEnterEditMode(); }