简体   繁体   中英

How do I replace the default text on an InputField in Unity?

When I create a new InputField on a screen and give it some default text, the text field returns nothing until I edit it. Looking at the debugger, the text field is actually "".

Once I edit the field at play time, it now equals what I input. But when I come back around to activate that screen again and I want to reset it, setting each of these fields to "" again does not show the original default text, it blanks them out. How do I reset the original text? Where does it live?

Input fields have 2 children attached: Placeholder with a Text component attached which says: Enter text... and a Text with a Text component which says nothing.

If you change the Text in the Text child you will have a default value, but this will show in place of the placeholder text.

It can be done in code if you want the placeholder to be shown when there.

Example:

public InputField inputField; //this is the input field that you are typing into.

public void ButtonPress()
{
    //when this button is pressed it will set the value or a default
    if(inputField.Text == "")
    {
        //insert default information
    }
    else
    {
        //enter the rest of the function here
    }
}

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM