- Home /
Auto layout and content fitter with InputField
I've been making a UI which is very dynamic. Part of it is that I have a vertical layout group containing multiple InputField
objects and these need to fit whatever content is put in by the user.
The Width is not variable and I need the input field to grow vertically based on the value string.
With a text field it works and I can insert any length string and the rect will grow downwards and make everything fit. Yet with an input field this does not really seem to be supported the same way.
After looking at it, it seems that the input field makes the text object just contain what is visible and the rest 'scrolls', so is not actually part of the Text.text
. So therefore my content fitter and vertical layout group only cause a preferred height of about 21, showing one line (input field is set to multi-line) as this is all that is really in the text field.
I really don't want to have to figure out how to write my own input field but could not find anyone else with the same problem. Is there a known solution for this? The simplicity is that I need to text child of the input field to not 'scroll' but contain all text at all times so my auto layout works.
Edit: yes it is wrapped. Also tried to not wrap it with no luck.
I noted that disabling and reenabling the LayoutElement resulted in it fixing. I just fixed the problem by adding this script onto the same GO as the inputfield:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// This is to update the element as otherwise the Inputfield will not update correctly and show the whole text. Instead, inbuilt functionality will 'scroll' the text (cut parts that 'dont fit' into the text field) and not resize the text.
/// </summary>
public class InputFieldHack : MonoBehaviour
{
LayoutElement element;
void Awake()
{
element = GetComponent<LayoutElement>();
#if UNITY_EDITOR
if(element == null) Debug.LogError("Element == null");
#endif
GetComponent<InputField>().onValueChanged.AddListener((value) => { HackTheSystem(null); } );
}
public void HackTheSystem(string value)
{
//Hack much?
element.enabled = false;
element.enabled = true;
}
}
This results in some sort of update that rescales everything correctly. Removing the script results in it breaking again as above.
Unity bug?
Answer by Wyattagum · Jun 10, 2018 at 06:06 AM
Use vector layout component for an auto layout
You mean LayoutElement? Tried that and does not work as that is not dynamic in this use case - you can only put in a hard value for preferred height, etc. The point is that the text does not display in text.text due to the 'scrolling' of the InputField which is done automatically and cannot be turned off as far as I can see.
Inputfield is just another UI with script. Try again.
You might not setup layout for Inputfield children correctly.
Also, you need auto-fit(Scale based on parent size) not Layout Element
Did you wrap it?
I just woke up so some of my answers are stupid