Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

InputField: onValueChanged missing?

Discussion in 'UGUI & TextMesh Pro' started by akyoto_official, Aug 21, 2014.

  1. akyoto_official

    akyoto_official

    Joined:
    Feb 24, 2013
    Posts:
    52
    Hmm, am I missing something or is there no onTextChanged or onValueChanged event for input fields (like onValueChanged for sliders)?

    I'm kind of awkwardly staring at my screen not knowing how to activate the login button once the inputfield text is valid (without constantly polling the text value). I checked the documentation for InputField but onValidateInput and onSubmit aren't helpful.
     
  2. bocs

    bocs

    Joined:
    May 9, 2009
    Posts:
    406
    Only took a quick look and maybe it will help until someone can give you a proper way.

    I put a simple script on the text object of the InputField.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class InputTest : MonoBehaviour
    7. {
    8.     public Text text;
    9.  
    10.     public void ValidateText()
    11.     {
    12.         //If valid enable login button...
    13.         Debug.Log (text.text);
    14.     }
    15. }
    16.  
    Add an "Event Trigger" to the InputField
    Add New, UpdateSelected
    setup object and function




    So this will call that function I think based on "Input Actions Per Second" constantly while the input field is selected.

    hacky, so maybe someone knows the correct way.
     
    akyoto_official and StarManta like this.
  3. _Adriano_

    _Adriano_

    Joined:
    Apr 4, 2013
    Posts:
    2
    Code (CSharp):
    1. public class InputTest : MonoBehaviour
    2. {
    3.     UnityEngine.UI.InputField field;
    4.     void Start ()
    5.     {
    6.         field.onSubmit.AddListener(new UnityEngine.Events.UnityAction<string>(OnSubmitField));
    7.         //Or
    8.         field.onSubmit.AddListener(new UnityEngine.Events.UnityAction<string>((string value) => { Debug.Log(value); }));
    9.     }
    10.  
    11.  
    12.     void OnSubmitField(string value)
    13.     {
    14.         Debug.Log(value);
    15.     }
    16. }
     
    Aestial, wula_think and rakkarage like this.
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Yes this is a bug that was fixed after b17 was already in the build process. It should be fixed for the next beta :)
     
  5. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    There is an onSubmit that exists on the InputField... but we forgot to include it in the inspector. We have added it now.
     
  6. akyoto_official

    akyoto_official

    Joined:
    Feb 24, 2013
    Posts:
    52
    @_Adriano_
    @Tim C

    Like I said in my initial post, onSubmit doesn't do the job. It is only triggered when pressing Return or the key set up in the Event System. I am looking for a way to be notified of a text value change instantly without polling, e.g. to activate / deactivate certain buttons.

    The most intuitive way would be by using the same system the slider component already uses in the inspector: onValueChanged.
     
    Last edited: Aug 21, 2014
  7. _Adriano_

    _Adriano_

    Joined:
    Apr 4, 2013
    Posts:
    2
    The class of UGUI can be inherited?

    Code (CSharp):
    1. using UnityEngine;
    2. using UGui = UnityEngine.UI;
    3. public class MyCustomInput : UGui.InputField
    4. {
    5.     protected override void Awake()
    6.     {
    7.         Debug.Log("<color=red> Custom </color>");
    8.         base.Awake();
    9.     }
    10. }
     
  8. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,304
    I agree that there should be an "OnValueChanged" right out of the box that we can register for in the editor.
     
  9. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    There are KeyUp, KeyDown, Changed etc. events defined for Event. It would be nice if InputFields emitted them. It would also be nice if you could get an Event object sent to your event handlers in a simple and obvious way.

    And Carthage should be burned.
     
  10. Greg-SS

    Greg-SS

    Joined:
    Jan 19, 2014
    Posts:
    2
    Well, here's a little hack...
    EDIT: changed to use UnityAction<> instead.

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine.UI;
    3.  
    4. public class CustomInputField : InputField
    5. {
    6.     public UnityAction<string> OnValueChanged;
    7.  
    8.     public override void OnUpdateSelected(BaseEventData eventData)
    9.     {
    10.         var old = this.value;
    11.         base.OnUpdateSelected(eventData);
    12.         if (this.value != old && OnValueChanged != null) {
    13.             OnValueChanged(this.value);
    14.         }
    15.     }
    16. }
     
    Last edited: Oct 2, 2014
  11. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    @Greg SS

    Use unityaction <> instead of custom delegate. Thanks for idea though
     
  12. jfarias

    jfarias

    Joined:
    Apr 8, 2014
    Posts:
    53
    Good news from b21:
    I still have a problem though. The event is not called when deleting a character.
    Is this the desired behaviour? Technically, when deleting a character, the value changed.

    For example, I'm using this in a search bar updating the results as the string is written. When deleting the last inserted character, it should call the OnValueChange callback so that I can update the results filtered by the remaining text but it's not being called.
    I've been using the OnUpdateSelected to work around this.
     
  13. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Added it to deleting chars as well. Its was a oversight by me :(
     
  14. jfarias

    jfarias

    Joined:
    Apr 8, 2014
    Posts:
    53
    No problem phil. Thanks for the fix. :)