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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Non-breaking space in Unity

Discussion in 'Scripting' started by Zante, Jan 13, 2011.

  1. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    I'm trying to figure out how to use non breaking spaces in Unity, the sort of thing you get with html and  

    I've tried \u00a0 which is supposed to be a unicode blank character but I'm not having much luck.

    Using javascript.

    Anyone in the know able to shed some light on this?
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    What is a non breaking space?

    Just putting a space into a string results in a space in any string you have, it won't break anything.
    If you talk about line breaks then create a new gui style that does not use wordwrap.
     
  3. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    I'm aligning a GUIlabel to the right of the screen. If I end a string with a space, Unity automatically removes that space and the rest of the text shifts to the right in order to reflect this fact.

    I'm using a custom text style with "word wrap" unchecked.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    in this case you will need to use GUI (not guilayout) to manually position the text accordingly I fear.
     
  5. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    I'd like to keep the text style as it makes setting a background image so much easier. It doesn't seem like a massive problem and that I'm sure there must be a solution which involves a "forced blank character".

    A visual aid:



    (there's actually a "." after the first line, but it's cut off in the pic)
     
    Last edited: Jan 13, 2011
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I'm pretty sure that Unity GUI actually uses the trim calls on strings before using them to generate the bitmap rendering to prevent useless calculations for something that won't show up yet would cause all sort of funny issues with auto alignement and guilayout.

    Wouldn't it be significantly easier and also more flexible to not use GUILayout and instead use GUI and specifying the corresponding rect? then you can just use an align right and have no probs at all and full control
     
  7. JilJill

    JilJill

    Joined:
    Nov 11, 2009
    Posts:
    129
    use '\n' when you want to make a line space in a gui
     
  8. SergeantBiscuits

    SergeantBiscuits

    Joined:
    Jul 22, 2012
    Posts:
    17
    I know this is old but I came across this thread with the same problem while searching; hoping I can help out others with the same question.

    In your script editor, Alt + 255 should generate a no-break whitespace.

    (Worked for me using FlashDevelop... should work for most.)

    Cheers,
    Wes
     
    arfish likes this.
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    On Macs, type "alt-space".

    --Eric
     
    arfish likes this.
  10. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,185
    Internally the text mesh generator ignores spaces at the end of a line for right and center aligned text (this is used for word wrapping so that spaces seemed stripped when text is wrapped on a space). To achieve this you should be able to use a unicode none breaking space as has been described above.
     
  11. The-Spaniard

    The-Spaniard

    Joined:
    Jan 7, 2012
    Posts:
    149
    I'm getting this very problem - my gui works fine if I pad out the beginning of strings with N*"_" but not with N*" ". How can one insert multiples of non-breaking spaces into strings? In windows, using monodevelop. Alt +255 doesn't work. Thanks for the help.
     
  12. Unit-E

    Unit-E

    Joined:
    Apr 12, 2012
    Posts:
    6
    Necroing this to save someone else the frustration. Alt+0160 and Alt+0255 didn't work for me, but putting "\r" after the spaces at the end of a string prevents Unity from stripping the spaces.
     
  13. tcrown

    tcrown

    Joined:
    Sep 5, 2014
    Posts:
    1
    This works!
     
  14. benio33

    benio33

    Joined:
    Jul 22, 2016
    Posts:
    3
    Hello everyone with my first post :)

    Could you please help me putting non breaking space into text field "text" value through GUI? I have a button name for example "My Button One" and when its width gets below certain threshold the text field on the button changes to:
    "My
    Button
    One"
    and I want to keep it:
    "My Button One"
    I tried with Horizontal and Vertical overflow options which keep the text like I want but doesn't looks good for every resolution. I did get it to work and it works exactly like what I need by putting this in the script:
    GameObject.Find("MyButtonOne").transform.GetChild(0).GetComponent<Text>().text = "My\u00A0Button\u00A0One";
    but the thing is I have plenty of buttons and it seems to be a bit overkill to have script changing your button names, besides I'd like to have real time feedback on how the buttons will look. Does anyone know how to achieve it in the Unity GUI typing in the "text" field on Windows? I read that on Mac it is Alt+Space. On Windows, I tried multiple combinations and non works.

    Thank you!
    Benio