- Home /
UI Input field that grows in size with each return line
How would I make a Input field box that grows in size with each return line?
i.e. say each line is currently being rendered at 30 px tall. If I have three lines I want it to be 90 px tall.
Answer by Diet-Chugg · Dec 11, 2017 at 09:16 PM
I solved it. (I found a link on google with part of this guide but I forgot where and I improved on the solution) Here's what I did:
I made a Text UI element and attached a Content Size Fitter to it.
Changed the Vertical Fit to Preferred size
Set the Alpha of the Text element to 0
Turned off Raycast Target to the Text element.
Added an input field as a child of the text object
Added the script InputFieldValidateTest to the Text Element
Added to the Event OnValueChanged on the InputField a link to the Text element's InputFieldValidateTest to call the dynamic string UpdateTest.
And you are good to go.
(Yes you should probably give the script and function a better name but i digress)
Here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InputFieldValidateTest : MonoBehaviour {
[SerializeField]
private Text _text;
public void UpdateText (string text)
{
_text.text = System.Environment.NewLine + text;
}
}
Your answer
Follow this Question
Related Questions
Catching get focused for UI InputField 0 Answers
Multiple Cars not working 1 Answer
Deactivate input field, NOT set interactable to false 1 Answer
Distribute terrain in zones 3 Answers
Hover Over Input Field Before Inputting? 2 Answers