Search Unity

Input filed - fit to content

Discussion in 'UGUI & TextMesh Pro' started by Rendolf, Nov 21, 2014.

  1. Rendolf

    Rendolf

    Joined:
    Aug 26, 2014
    Posts:
    19
    In unity 4.6 there is no something like text area. I use input filed (multiline new) nad scroll rect. I need input filed change its height when there is to much text. I tried to use content size fitter component but it doesn't work when i write to input field in playmode (it works when I write directly in inspector). Is this issue ?
     
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    nope its by design, the input field will only display the amount of text that will fit. the content size fitter looks at the text and see that it can always fit. So its never expanded. you'd have to do your own thing to get it to expand
     
  3. Rendolf

    Rendolf

    Joined:
    Aug 26, 2014
    Posts:
    19
    Mhm, I understand. Do you plan to add text area to modern UI? It was present in old UI.
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    You can create a text area fine, just take a regular InputField and make it larger and set it to multi-line. It just won't automatically adjust its size to the amount of content.
     
  5. Rendolf

    Rendolf

    Joined:
    Aug 26, 2014
    Posts:
    19
    Ok, but is there any way to make input filed works with scrollbar? Using keyboard to searching is not very intuitive.
     
  6. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    No thats not currently supported out of the box you'd need to roll your own solution
     
  7. Rendolf

    Rendolf

    Joined:
    Aug 26, 2014
    Posts:
    19
    Ok, thx for answer :)
     
  8. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    I have something for this, it's clunky but kinda works, I'll post later.
     
  9. Rendolf

    Rendolf

    Joined:
    Aug 26, 2014
    Posts:
    19
    Ok, I am still interested :)
     
  10. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
  11. Rendolf

    Rendolf

    Joined:
    Aug 26, 2014
    Posts:
    19
    It works, thanks :) Have you tried to use this to expand Text that is Text component in input field? I try to do that and the caret is under this Texts rect.
     
  12. illa3d

    illa3d

    Joined:
    Dec 7, 2014
    Posts:
    17
    I've had a simmilar issue and compiled a workaround until Unity fixes the InputField functionality.
    All the combinations of layous/content fitters didn't yield proper display of the last line.
    The only downside is that you can't fix the height of the input field without a ScrollRect component, also in the example.

    Also, this example doesn't cover the ScrollRect following the caret nor the selection in the InputField, if someone does that please post it here! :)

    HACKY SOLUTION:
    InputFieldMod.cs - http://pastebin.com/AHggHUMX
    Example - http://illa3d.com/unity/inputfield_mod_v1.0.unitypackage
     

    Attached Files:

  13. Fragmental

    Fragmental

    Joined:
    Jun 30, 2013
    Posts:
    61
    sj631 likes this.
  14. Fragmental

    Fragmental

    Joined:
    Jun 30, 2013
    Posts:
    61
    That's working great so far. Thank you!
     
  15. Fragmental

    Fragmental

    Joined:
    Jun 30, 2013
    Posts:
    61
    After more testing it seems as though it works great in the Editor, but in my Android build it is not working as well. The input field still expands, but it expands much more than it should. A lot more.

    Could it be because I'm using a modified canvas scaler? It scales by DP.

    Actually, now that I check, it seems to expand incorrectly with all but Constant Pixel Size(and my DP Canvas Scaler) in the editor. I haven't test them all in the android build, because it requires creating a new build for each test, to test properly.
     
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    What are you trying to fit it into?
     
  17. Fragmental

    Fragmental

    Joined:
    Jun 30, 2013
    Posts:
    61
    https://www.evernote.com/l/ATqApPu-8NdMB6EtnFk1gW64aCNpwyx16QoB/image.png
    here's a picture of it. This is using Constant Physical Size in the Canvas Scaler. It adds just a little bit too much space on each line. This is what happens when I'm using my DP Canvas Scaler script on mobile, only it's maybe about 10 times worse(maybe more)?
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not sure what your script does, but I just tried an inputfield test with constant physical size and I could get it to adjust
    I'd like to try to help, if you're still looking, and if I can.
    Is this just an InputField that you'd like to have its size adjust based on its content?
     
  19. Fragmental

    Fragmental

    Joined:
    Jun 30, 2013
    Posts:
    61
    I'll try to make a simple test example scene that I can share with you to show what is happening.

    Wow, I just realized, in 5.6 the example scene in illa3d's unity package is very broken.
     
  20. Fragmental

    Fragmental

    Joined:
    Jun 30, 2013
    Posts:
    61
    It is an inputfield that I'd like to have it's size adjust based on its contents.
    Here is a package with an example scene. I stripped out all the unimportant parts. Once you load the scene you can click the pencil to show the input field, and as you can see, with canvas scaler set to constant physical size, there is significant white space below the last line of text.
    https://www.evernote.com/l/AToSVrGDU6BPTbOjNAtmnfxUmIedRa_5pQYB/image.png
     
  21. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, I am looking at your package. When I'm in the editing mode, it looks like there is extra space, but when I switch the edit mode off, it all lines up perfectly. The edit mode panel has a preferred height set to 265 which is what it shows when I open it on mine.
    Those are just some thoughts. :)
     
  22. bkachmar

    bkachmar

    Joined:
    Mar 15, 2013
    Posts:
    43
    This is my setup to make a scalable input field:
    Regular UI Text field with ContentSizeFitter vertical set to Preferred size and unchecked Raycast target.
    As a child I add Input Field as Multi Line with with RectTransform "stretch" alignment, so that it takes the full size of it's parent.
    The Text child of Input Field has the color set to invisible, so that it is not displayed at all.
    And on Input Field Value Changed I set it's value to my parent Text.

    With this setup when I type something inside the input field it is copied to it's parent "visible" text that is scalable.
     
    confy and unity_VvCODKknmBJQGw like this.
  23. blamejane

    blamejane

    Joined:
    Jul 8, 2013
    Posts:
    233
    I see your solution in a lot of posts. I've tried it, but I don't see the input caret. Is the caret lost with your solution @bkachmar, or am I doing something wrong? Also, am I correct in assuming that I need to use a script (as opposed to making connections in the inspector) to copy the contents of the input field text to the parent text?

    UPDATE: So while I was waiting I decided to switch which Text component to make invisible. According to my understanding of your steps, the InputField child Text game object should be made "clear" (invisible). But doing this I lose the caret. I reset the color for this InputField child and made the Text component of the UI Text (parent) invisible. Now I get the exactly what I wanted, which is an auto-sizing inputfield. This should be a unity standard ui component. Thanks!!!! P.S. I still need to test on the device, but it seems to be working great in the editor.
     
    Last edited: Nov 1, 2017
    VhipCodes likes this.
  24. bkachmar

    bkachmar

    Joined:
    Mar 15, 2013
    Posts:
    43
    @blamejane, I lost a curret in my case. I had it visible in some cases, but not all the time, so now there is no curret at all.
    But that's not a problem, because on a device there is a separate native input field coming out together with a keyboard.
     
  25. unity_VvCODKknmBJQGw

    unity_VvCODKknmBJQGw

    Joined:
    Aug 13, 2020
    Posts:
    7

    This answer works 100%. Only problem I had is the text marker (where I am currently typing) - if anyone has a solution I'd love to hear it :)