Search Unity

Unity UI 4.6 inputfield bug or behavoir?

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by unityplease, Jan 5, 2015.

  1. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    I'm having problems with unity inputfields keep taking keyboard input after I have pressed return.

    I have written three questions regarding this on unity answers but so far not got any positive replies.

    The issue is:

    Once an input field has been clicked on, and text entered. If it is then submitted by pressing enter or by clicking on another game object, if I then move my character controller around a bit the input field will keep picking up the WSDA keys or anything else I press.

    Also, if there is more than one input field, the focus will randomly switch from one input field to another after I press submit or move around a bit.

    The questions detailing this are here:

    http://answers.unity3d.com/questions/867313/ui-46-input-field-how-do-i-use-the-onendedit.html

    http://answers.unity3d.com/questions/867836/46-inputfield-strange-behaviour-after-submit.html

    http://answers.unity3d.com/questions/867765/deactivate-input-field-not-set-interactable-to-fal.html

    Please help this is really holding up my project.

    Many thanks in advance.

    If it is a bug please let me know.
     
  2. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Anyone? I've tried this on three separate computers and I get the same behavior. This is a rather major bug if there isn't something simple I'm missing.
     
  3. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    So is it a bug do I need to report it?
     
  4. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    Enter doesn't make the input field lose focus. Have you considered just disabling the input field? If you're using WASD for movement I'm going to guess the input field is probably not on the screen or being used, yes? You can just do the InputField.enabled = false and then reenable it later if you want input to go in.

    Basically what is happening is you aren't switching to some other control so the input field is still taking in put. If you have multiple input fields it might be useful to have more than one submit button per field or one big submit button that just turns them all off when you're done.
     
  5. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    The input fields are on a canvas in world space, and there are many of them floating above objects. However this bug also manifests on non world space inputfields if you have several on screen. Once you have clicked on one, when you press return it will just focus another one randomly. Go on try it.

    Disabling them isn't an option because I want them to be able to still be edited when someone clicks on one and I just want them to go back to the state they were in once someone has pressed return. I dont' want to switch interactable to false because this means I won't be able to click them again.
     
  6. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
    pressing enter just stops the editing it never deselects the graphic. If you want to deselect it, listen for the onsubmit event and then call EventSystem.current.SetSelectedGameObject(null);
     
  7. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    That doesn't appear to work please look at this code:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    using UnityEngine.EventSystems;

    public class CreateCanvasButton : MonoBehaviour {




    void Start () {
    // create event system
    GameObject eventsystem = new GameObject ();
    eventsystem.AddComponent<EventSystem> ();
    eventsystem.AddComponent<StandaloneInputModule> ();
    eventsystem.AddComponent<TouchInputModule> ();


    string name = "Canvas";

    GameObject newCanvasGO = new GameObject ();
    newCanvasGO.name = name;
    newCanvasGO.transform.SetParent (this.transform);
    newCanvasGO.AddComponent <Canvas>();
    newCanvasGO.AddComponent<CanvasScaler> ();
    newCanvasGO.AddComponent<GraphicRaycaster> ();

    RectTransform theCanvasRectTransform = newCanvasGO.GetComponent<RectTransform> ();
    theCanvasRectTransform.anchoredPosition3D = new Vector3 (0f, 0f, 0f);
    theCanvasRectTransform.sizeDelta = new Vector2 (2400f,3800f);
    theCanvasRectTransform.anchorMin = new Vector2 (0f,0f);
    theCanvasRectTransform.anchorMax = new Vector2 (1f,1f);
    theCanvasRectTransform.localScale = new Vector3 (0.0005f,0.0005f,1f);
    theCanvasRectTransform.localPosition = new Vector3 (0f, 2.9f, 0f);
    Canvas theCanvas = newCanvasGO.GetComponent<Canvas> ();
    theCanvas.worldCamera = Camera.main;
    newCanvasGO.SetActive (true);
    newCanvasGO.AddComponent<Image> ();
    Image canvasImage = newCanvasGO.GetComponent<Image> ();
    Sprite mySprite = Resources.Load<Sprite> ("Background");
    canvasImage.sprite = mySprite;
    canvasImage.type = Image.Type.Sliced;
    // create name title text
    GameObject titleTextGO = new GameObject ();
    titleTextGO.name = "Name_text";
    titleTextGO.transform.parent = newCanvasGO.transform;
    Text titleText = titleTextGO.AddComponent<Text> ();
    RectTransform titleTextRT = titleTextGO.GetComponent<RectTransform> ();
    titleTextRT.anchoredPosition3D = new Vector3 (0f,0f,0f);
    titleTextRT.sizeDelta = new Vector2 (877,199);
    titleTextRT.localPosition = new Vector3 (-22f,1722f,0f);
    titleTextRT.localScale = new Vector3 (1f, 1f, 1f);
    titleText.text = "Title";
    Font theFont = Resources.GetBuiltinResource<Font> ("Arial.ttf");
    titleText.font = theFont;
    titleText.fontSize = 140;
    titleText.color = Color.black;
    titleText.alignment = TextAnchor.MiddleCenter;
    titleText.resizeTextForBestFit = true;
    titleText.resizeTextMinSize = 10;
    titleText.resizeTextMaxSize = 160;
    // create nickname title text
    GameObject TextGO = new GameObject ();
    TextGO.name = "InputField";
    TextGO.transform.parent = newCanvasGO.transform;
    TextGO.AddComponent<RectTransform> ();
    RectTransform TextRT = TextGO.GetComponent<RectTransform> ();
    TextGO.AddComponent<InputField> ();
    InputField myInput = TextGO.GetComponent<InputField> ();
    TextRT.anchoredPosition3D = new Vector3 (0f,0f,0f);
    TextRT.sizeDelta = new Vector2 (1669,3370);
    TextRT.localPosition = new Vector3 (-35f,-188f,0f);
    TextRT.localScale = new Vector3 (1f, 1f, 1f);
    myInput.transition = Selectable.Transition.None;
    myInput.text = "SomeText that never appears";


    myInput.onEndEdit.AddListener(delegate{StopInput(myInput);});


    GameObject textForInputGO = new GameObject ();
    textForInputGO.name = "Text_ForInputField";
    textForInputGO.transform.SetParent(TextGO.transform);
    RectTransform textRT = textForInputGO.AddComponent<RectTransform> ();
    Text textscript = textForInputGO.AddComponent<Text>();
    textRT.sizeDelta = new Vector2 (1356f,300f);
    textRT.localPosition = new Vector3 (0f,-0f,0f);
    textRT.localRotation = new Quaternion (0f, 0f, 0f, 0f);
    textRT.localScale = new Vector3 (1f,1f,1f);
    myInput.textComponent = textscript;
    textscript.supportRichText = false;
    textscript.resizeTextForBestFit = true;
    textscript.resizeTextMaxSize = 300;
    textscript.resizeTextMinSize = 10;
    textscript.font = theFont;
    textscript.text = "This is the main body text.";
    textscript.color = Color.black;
    textscript.alignment = TextAnchor.MiddleCenter;


    // create button
    GameObject invisibleButtonGO = new GameObject ();
    invisibleButtonGO.name = "invisibleButton";
    invisibleButtonGO.transform.parent = newCanvasGO.transform;
    invisibleButtonGO.AddComponent<RectTransform> ();
    RectTransform invisibleButtonRT = invisibleButtonGO.GetComponent<RectTransform> ();
    invisibleButtonRT.anchoredPosition3D = new Vector3 (0f,0f,0f);
    invisibleButtonRT.sizeDelta = new Vector2 (358,354);
    invisibleButtonRT.localPosition = new Vector3 (1021f,-1723f,0f);
    invisibleButtonRT.localScale = new Vector3 (1f, 1f, 1f);
    invisibleButtonGO.AddComponent<Button> ();
    Button invisibleButton = invisibleButtonGO.GetComponent<Button> ();
    invisibleButton.transition = Selectable.Transition.None;
    invisibleButton.onClick.AddListener (() => handleButton());

    //add image for ray casting invisable button:
    Image image = invisibleButtonGO.AddComponent<Image>();
    image.color = new Color(0, 0, 0, 0);

    }

    void handleButton (){
    print ("pressed!");
    }

    void StopInput(InputField _input) {
    print (_input.text);
    EventSystem.current.SetSelectedGameObject(null); // neither does this
    }


    }


    1) If I select another object rather than pressing return I get "Attempting to select while already selectring an object" error.

    AND

    2) It requires me to press return twice before it stops accepting input.
     
    Last edited: Jan 7, 2015
  8. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Any ideas ?
     
  9. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
    I'd file a bug report and we can take a better look, i dont see anything wrong with the code.
     
  10. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Bug report submitted.
     
  11. acmsharBAH

    acmsharBAH

    Joined:
    Jan 8, 2015
    Posts:
    6
    Phil, since this is timely, I thought I should ask here. I have a similar problem. When I select the text of an input field via script, that input field is then selected until a player deselects it by clicking elsewhere. Is that the correct default behavior?

    What I am trying to do is have the player click a button that fills in the input fields that are onscreen, then use WASD to walk around while those input fields are still available for them to use if they want to eventually change their selection. I am currently using the solution you suggest to deselect the UI items through the event manager, but I'm wondering if there is a good reason for this being the default when an input field is edited via script.
     
  12. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
    well its the same behaviour as all other components its just visible with input field. pressing a button selects it, you will only get the deselect when you navigate away or press something else.

    Changing the text of a input field via script shouldn't activate it. so i'm not sure what your refering to.
     
  13. acmsharBAH

    acmsharBAH

    Joined:
    Jan 8, 2015
    Posts:
    6
    I am saying that changing the text via script does seem to activate it 4.6.1f1 for me. If I call EventSystem.current.SetSelectedGameObject(null) after I change the text field in the script it will not record WASD as I move. But if I do not call that, it will record my keystrokes even if I never select the input field in the game (only via the script that changed the value of the text).
     
  14. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
    interesting could you file a bug on that, quick look at the code doesnt seem like it should activate but maybe something weird is going on.
     
  15. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Have you had a chance to look at my bug report yet ?
     
  16. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
    I dont know which is yours, and they always go through QA to verify first. So i'm guessing no.
     
  17. acmsharBAH

    acmsharBAH

    Joined:
    Jan 8, 2015
    Posts:
    6
    I submitted a bug report for my issue. #662033
     
  18. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,284
    What's the point submitting bugs? They never fix them.
     
    MrEsquire likes this.
  19. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    I hope they will look at this inputfield bug/behavior because I've been dealing with this for 4 weeks now and its really holding up my project.
     
    MrEsquire likes this.
  20. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Looking forward to resolution of the inputfield issues.

    Just while you wait to process the bug report you can re-create these issues by adding multiple inputfields into a blank scene and then adding in a plane with collider and First Person Controller to move around.

    Then just trying clicking on one input field and then press return and move around a bit. You'll see what I'm taking about.
     
  21. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
    So just took a look at this bug. Its not a bug with the input field. The issue is the navigation. Pressing the button sets it as active. You then press wasd which are navigation keys. the navigation layout then points from the button to the input field causing the input field to be selected and activated. You need to change your navigation properties.
     
  22. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Is his bug the same as mine? Doesn't sound exactly like it. Do you have an example of it and how to deal with it.
     
  23. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
    no i think your bug is different. Could you post the bug #'s you submitted here and i can take a look directly at them now instead of waiting on QA.

    His bug was due to navigation. If you select any element there is a "navigation" property. This is usually set to automatic. From there you can hit visualize and in the scene it will show you where each element will go for the navigation keys if you follow the yellow line. If something isn't going where you want you can set it to manual and then set the navigation that way.
     
  24. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    I couldn't find the verification email after the bug report was sent, but it did send as far as I know. I just just sent another one in case the first didn't go through.

    The bug report copy I filed today is
    (Case 662880) Selecting an inputfield allows the input of characters.

    The title isn't a good one, maybe it was truncated or maybe I rushed the title :(
     
  25. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
  26. acmsharBAH

    acmsharBAH

    Joined:
    Jan 8, 2015
    Posts:
    6
    Thanks, Phil. I was unaware of the UI navigation default.
     
  27. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Any chance to look at that bug report yet ?
     
  28. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
    Yea i did. QA closed it before i got to it, but i did fix 1 issue with it. But another part of your issue is also the navigation default.
     
  29. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Thanks Phil for taking the time really appreciate it. I'll take a look and see if my problems are solved.

    Do you have a link to the resolution; I can't seem to find it.
     
  30. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Please let me know what the issue was and how I can fix it thanks.
     
  31. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
    So one issue was the active type flag in the input was being set to late so deselecting in the onEndEdit would cause another onEndEdit to fire. thus getting you into a loop of setting selection multiple times.

    The other issue is you should try to wrap your selection calls in a EventSystem.current.alreadySelecting
     
  32. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Are there some examples of this that I can read? It would be great if they were in relation to the scripted inputfield.
     
  33. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,159
    I dont know of any examples.

    but what i mean is before you call EventSystem.current.SetSelected() but a if (!EventSystem.current.alreadySelecting) this is prevent the double selection from happening.
     
  34. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Any chance you could like me to the bug report I submitted, the inputfields still trigger randomly after I have activated one of them and I have no idea how to stop this.
     
  35. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    It would be great if someone who knows what they are doing could provide a comprehensive tutorial for scripting inputfield s. The documentation is very sparse with no examples. This behavior of inputfields being activated and then randomly reactivating after pressing submit/enter or clicking somewhere else is really annoying.

    I've been trying to make this work for two months now. Other people must be having problems with this also.

    I really need clarification of the code for deactivating the inputfield when submit is pressed or another game object is clicked on.
     
  36. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    Some day an answer will come ... I can only hope and cry in the mean time.

    And just for clarification the inputfield still randomly picks up keypresses when I move around the scene after it has been submitted.
     
    Last edited: Feb 14, 2015
  37. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    31
    I PARTIALLY solved the problem by adding the following script to the inputfield gameobject

    using UnityEngine;
    using System.Collections;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;

    public class InputFieldClickHandler : MonoBehaviour, IPointerClickHandler, IDeselectHandler {


    private InputField inputfield;

    void Start () {
    inputfield = gameObject.GetComponent<InputField> ();
    }

    public void OnPointerClick(PointerEventData eventData)
    {
    inputfield.interactable = true;
    }

    }

    And on the inputfield itself it has:


    inputField.onEndEdit.AddListener(delegate {StopInput(inputField);});


    void StopInput(InputField _input) {
    _input.interactable = false;
    // contains more code for handing the text input and doing things with it.
    }


    This has the effect of stopping the inputfield from being interactable when it is deselected but re-enables it when it is clicked. However this means to edit it it is clicked twice. This all seems like a lot of extra code that shouldn't be needed if the inputfield just worked as desired from the start.

    I also find the inputfield occasionally triggers itself if I am using a scrollrect with a series of buttons on in the worldspace canvas and I am moving around the scene.
     
  38. _unitysam_

    _unitysam_

    Joined:
    Dec 20, 2014
    Posts:
    1
    5 years late on this post, but I was running into this issue a lot too and I think I found a simple workaround for my situation. I had a bunch of input fields where the player can make notes. However after hitting enter, random fields would be re-selected when the player hit W,A,S,D to move.

    To solve it, every time the player hits enter (or mouse clicks off the input field) I just select a dummy UI element I made. Once the selection was off the input fields entirely it stopped the WASD keys from re selecting them.

    Note, Im actually using Playmaker so instead of the written script I used the action called 'U Gui Set Selected Game Object' on each of the input field FSMs.
     
unityunity