Textfield clear on input for mobile

Hi,

trying to implement the thing where the textfield has a default value and will clear the value when the user wants to type something in. Below is the code snippet used

void Start()
{
   myTextField.RegisterCallback<FocusInEvent>(ev => focusIn());
   myTextField.RegisterCallback<FocusOutEvent>(ev => focusOut());
}

void focusIn()
{
   if (myTextField.text.Equals(defaultTxt))
      myTextField.value = "";
}

void focusOut()
{
     if (string.IsNullOrEmpty(myTextField.value))
          myTextField.value = defaultTxt;
}

Works fine in play mode, but when deployed to a mobile device like android I notice that when u press on the textfield it would call FocusInEvent and then FocusOutEvent immediately when the on screen keyboard pops up.

Any other way to implement this?

Try using myTextField.SetValueWithoutNotify("");

Ah was not aware of that method.

But doesn’t help with the FocusOutEvent being called when the on screen keyboard appears.

Tried using OnApplicationFocus but sometimes the textfield clears the input by itself… think theres something with the timing of things executing I’m not accounting for

Maybe I’ll just wait for them to implement placeholder text… lol 2022 eh?

I think placeholer text (among most useful features for input fields) is scheduled for 2023 :confused:
Don’t remember where I read it, but will drop a link if I find it. Have not tested for mobile because all input is broken on Unity Remote, but I do have a solution for placeholder text that at least works for PC.

Here is the link . It was 2022.1, not 2023.x.

Yea I can get it to work on PC, just not mobile. Just tried iOS… it treats OnApplicationFocus differently from Android. Its a real mess, guess I really do have to wait.

Ah! phew lol. Thanks a lot for your help!