public InputFieldUnityFloat(TextComp textComp, UnityAction <string> editEndCallback) : base(textComp, editEndCallback) { this.linkedTextComp = textComp; UnityAction <string> action = new UnityAction <string>(UpdateTextWhileTyping); this.inputField.GetComponent <UnityEngine.UI.InputField>().onValueChanged.AddListener(action); string path = RuntimeTextEdit.packagePath + "/Resources/InputFieldPanel.prefab"; GameObject panelPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)); if (panelPrefab == null) { LogSystem.LogWarning("Failed to locate prefab at path: " + path); return; } GameObject panel = Instantiate(panelPrefab); this.inputField.GetComponent <RectTransform>().offsetMin = new Vector2(10, 10); this.inputField.GetComponent <RectTransform>().offsetMax = new Vector2(-10, -10); GameObject canvas = GetCanvas(); panel.transform.SetParent(canvas.transform, false); this.inputField.transform.SetParent(panel.transform, false); this.inputField = panel; }
public InputFieldUnity(TextComp textComp, UnityAction <string> editEndCallback) { GameObject inField = (GameObject)AssetDatabase.LoadAssetAtPath(RuntimeTextEdit.packagePath + "/Resources/InputField.prefab", typeof(GameObject)); if (inField == null) { LogSystem.LogWarning("Failed to get input field prefab."); return; } this.inputField = Instantiate(inField); this.linkedTextComp = textComp; this.inputFieldComp = this.inputField.GetComponent <UnityEngine.UI.InputField>(); this.inputFieldComp.onEndEdit.AddListener(editEndCallback); UnityAction <string> action = new UnityAction <string>(UpdateTextWhileTyping); this.inputFieldComp.onValueChanged.AddListener(action); if (textComp.GetType() == typeof(TextCompUnityUI)) { MakeInputFieldHaveSameAppearance(); void MakeInputFieldHaveSameAppearance() { Preset preset = textComp.GetPresetOfInternalComp(); UnityEngine.UI.Text inputFieldTextComp = this.inputField.transform.Find("Text").GetComponent <UnityEngine.UI.Text>(); preset.ApplyTo(inputFieldTextComp); } } return; }
/// <summary> /// Creates a Textlet, which is a moveable piece of text. (TODO: font) /// </summary> /// <param name="text"></param> /// <returns></returns> public static Entity CreateTextlet(string text) { Entity e = CreateDrawlet(); e.AddComponent(new ScaleComp()); TextComp tc = new TextComp(text); tc.Font = _game.Content.Load <SpriteFont>("TTDebugFont"); // FIXME allow other fonts e.AddComponent(tc); e.Refresh(); return(e); }
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; }
void Start() { SearchManager(); void SearchManager() { UnityEngine.Object manager = UnityEngine.Object.FindObjectOfType(typeof(RuntimeTextEditManager)); if (manager == null) { LogSystem.LogWarning("Could not find RuntimeTextEditManager. Please add a RuntimeTextEditManager component to your scene. Can not continue."); return; } this.manager = ((RuntimeTextEditManager)manager); this.manager.RegisterComponent(this); } if (RuntimeTextEdit.packagePath == null) { RuntimeTextEdit.packagePath = ""; LocatePackagePath(); } SearchTextComp(); void SearchTextComp() { bool foundTextComp = false; UnityEngine.UI.Text textCompUnityUI = this.transform.GetComponent <UnityEngine.UI.Text>(); if (textCompUnityUI) { this.textComp = new TextCompUnityUI(textCompUnityUI); foundTextComp = true; } if (!foundTextComp) { TextMeshProUGUI textCompTextMeshProUGUI = this.transform.GetComponent <TextMeshProUGUI>(); if (textCompTextMeshProUGUI) { this.textComp = new TextCompTextMeshProUGUI(textCompTextMeshProUGUI); foundTextComp = true; } } if (!foundTextComp) { TextMeshPro textCompTMP = this.transform.GetComponent <TextMeshPro>(); if (textCompTMP) { this.textComp = new TextCompTextMeshPro(textCompTMP); Collider collider = textCompTMP.transform.GetComponent <Collider>(); if (collider == null) { LogSystem.LogWithHighlight("Text component is missing a collider. Needed for RuntimeTextEdit to work. Added a box collider.", textCompTMP); BoxCollider boxCollider = textCompTMP.gameObject.AddComponent <BoxCollider>(); boxCollider.center = Vector3.zero; boxCollider.isTrigger = true; RectTransform rectTrans = this.GetComponent <RectTransform>(); if (rectTrans == null) { LogSystem.LogWarning("No RectTransform component found!"); return; } boxCollider.size = new Vector3(rectTrans.sizeDelta.x, rectTrans.sizeDelta.y, 0); } foundTextComp = true; } } if (!foundTextComp) { UnityEngine.TextMesh textCompUnity3D = this.transform.GetComponent <UnityEngine.TextMesh>(); if (textCompUnity3D) { this.textComp = new TextCompUnity3D(textCompUnity3D); Collider collider = textCompUnity3D.transform.GetComponent <Collider>(); if (collider == null) { LogSystem.LogWithHighlight("Text component is missing a collider. Needed for RuntimeTextEdit to work. Added a box collider.", textCompUnity3D); BoxCollider boxCollider = textCompUnity3D.gameObject.AddComponent <BoxCollider>(); boxCollider.isTrigger = true; } foundTextComp = true; } } if (!foundTextComp) { LogSystem.LogWithHighlight("Could not find text component in GameObject: " + this, this); return; } } void VerifyTextIsRaycastTarget() { if (textComp.IsRaycastTarget() == false) { LogSystem.LogWithHighlight("Text component on GameObject (" + textComp.GetGameobjectName() + ") is not a raycast target. Clicking on this text component will not trigger RuntimeTextEdit.", this); // @TODO If not a raycast target, register callback on OnArm and OnDisarm that makes it a raycasting target. } } VerifyTextIsRaycastTarget(); /// Search for script that implements TextEditCallback SearchTextEditCallback(); void SearchTextEditCallback() { if (textEditCallback == null) { textEditCallback = this.gameObject; } } manager.DeactivateMe(this); }
/// <summary> /// Create a new FrameRateCounter script that modifies the text in the /// TextComp of the Entity, to show the FPS count. /// </summary> /// <param name="comp">The TextComp to modify</param> public FrameRateCounter(TextComp comp) { this.textComp = comp; }