Question by
ADasein · Sep 02, 2019 at 06:17 PM ·
uiinputfield
How to change/format the value displayed in an input field
I have an input field set as accepting only integers with a character limit of 6. I would like to when the content is being edited, the displayed value shows in currency format. So if the value is "2", the output is "$2.00" and so on.
My current code is not working.
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
public InputField Field;
public void Start ()
{
Field.onValueChanged.AddListener(delegate{ApplyCurrencyFormat();});
}
public void ApplyCurrencyFormat ()
{
Field.textComponent.text = "$" + Field.text + ".00";
}
}
I can show "$" + Field.text + ".00";
in the console but I can't change the output text.
Comment
I would use another text component over the real input field. Disable the original one, keep your listener and set the value of the other text, ins$$anonymous$$d of Field.textComponent
.