Unity Community

Register or Sign In:

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 40

  1. Location
    Amsterdam
    Posts
    301

    Text!

    This product is now available on the Asset Store.

    So I made my own solution for sprite text, like many did before. Started simple, but you know how it goes... So what features are we looking at here?

    Imports BMFont FNT file plus PNG, kerning data included. There are multiple free tools to convert TTF to this format.

    Name:  text_justify.png
Views: 1102
Size:  49.5 KB

    Justify alignment, along with the usual Left, Center, and Right modes. Achor at the default nine positions, plus baseline. Real tabs. Word wrap.

    Name:  text_maps.png
Views: 1102
Size:  61.5 KB

    A distance map generator, using the Anti-aliased Euclidean distance transform algorithm described by Stefan Gustavson and Robin Strand. It extracts nice distance maps from low-res anti-aliased textures. With this, you can get good quality graphics and effects, as described in "Improved Alpha-Tested Magnification for Vector Textures and Special Effects" by Chris Green from Valve (SIGGRAPH 2007).

    Name:  text_glyph_sizes.png
Views: 1098
Size:  70.2 KB

    It can handle weirdly sized glyphs just fine. If they overlap, later sprites are drawn on top of previous ones.

    Name:  text_bmp.png
Views: 1097
Size:  45.9 KB

    All Basic Multilingual Plane characters are supported. Basically, anything that fits into one .NET's two-byte char value. Of course, you need a font that includes the characters you want.

    Name:  text_inspector.png
Views: 1102
Size:  23.9 KB

    It's fully functional in editor mode, just like Unity's own TextMesh.

    The text component uses a regular mesh and acts as its own little sprite manager. The mesh data is fully accessible, so you could do post-processing on it if you want to. Performance is good, there's no temporary object allocation going on while sprites are being positioned and only the mesh data is stored.

    Now What?

    It still needs polish and I'm working on a few more things, including StringBuilder support for highly dynamic text, some post-processors, shaders, and helpers.

    So, would anyone buy this if I put it on the Asset Store? If I do so, I really don't know what to call this thing yet... Too generic a name will clash with other stuff out there, like EZgui's SpriteText. The lack of namespaces really hurts here. Would a company prefix be acceptable, like a plain CCText?
    Last edited by Jasper Flick; 02-28-2012 at 03:29 AM.


  2. Location
    Thessaloniki, Greece
    Posts
    518
    Interesting. Can it wrap strings as well?


  3. Location
    Amsterdam
    Posts
    301
    Yes, it word-wraps! I just realized I didn't mention that. You beat me to it!


  4. Location
    Netherlands
    Posts
    237
    Nice! How is the performance on mobile devices?
    The StringBuilder sounds interesting!


  5. Location
    Amsterdam
    Posts
    301
    There's some overhead whenever you update the text, beyond that it performs like any other mesh on mobiles. So keep triangle count (non-whitespace character count) down, don't use expensive shaders, keep the texture small, and update as little as possible. If you don't change the text, the thing just sits there and does nothing.

    In case you do want to update frequently, I did just add StringBuilder support. This really helps because it obviates constant allocation of string objects, which would otherwise pollute memory and cause garbage collection runs.

    I also made an example post-processor that modifies vertex colors.

    Name:  text_colored.png
Views: 1011
Size:  20.0 KB

    It adds a little gradient effect and gives everything that's between square brackets an alternative color. The brackers aren't shown because I excluded them from the font definition. The full text reads "You should visit [grandma]! She lives in the [house] near the [beach]."
    Last edited by Jasper Flick; 01-28-2012 at 07:41 AM.


  6. Location
    Amsterdam
    Posts
    301
    I think I'll just call this thing "TextBox".

    The boxes have a fixed width, which enables word wrapping. Height is dynamic. I made two bounding modes that control how the text is placed. In "Caret" mode, the text caret is constrained to the specified width, but oversized characters can stick out of it. In "Margin" mode, the text is padded to prevent the characters from poking out. Depending on the font, both modes can be equal or very different.

    Name:  text_bounding_caret.png
Views: 959
Size:  16.1 KB Name:  text_bounding_margin.png
Views: 959
Size:  16.0 KB

    I also added some helpers to get ints and floats into StringBuilders without causing string pollution.

    Name:  text_formatted_float.png
Views: 960
Size:  25.4 KB



  7. Location
    Thessaloniki, Greece
    Posts
    518
    What are you using to display the text? TextMesh?


  8. Location
    Redmond, WA, USA
    Posts
    56
    I would be interested in buying this if:

    1) It can produce nice, crisp text in both orthographic and perspective views (sounds like it can since you're using that Valve technique).
    2) It can mix font types/colors in one string (e.g. bold the first letter of a word and make every other word green). It's super convenient to not have to manually position multiple objects to get these kinds of effects.
    3) You have a good solution for displaying numbers that change very frequently (scores and timers) without generating garbage (either through an advanced string builder or by taking in an array of characters). That's the one pain point I'm hitting with ex2D's fonts is that my score changes frequently (every frame since it's a ticker-style score) but each time I generate a new string and thus garbage.

    As an aside you can wrap a namespace around C# classes in Unity. I do this for a few of my types to avoid collisions with other people's code. It's not required, but it's definitely supported. That said I don't know how that works when using Javascript or Boo. That said I also don't mind the prefix approach since nobody seems to use namespaces and, after using the iOS SDK, I've gotten kind of used to it.


  9. Location
    Amsterdam
    Posts
    301
    Diviner: The component uses a normal Mesh, MeshFilter and MeshRenderer.

    nickgravelyn:
    1) Sure, it can do that!

    2) It's limited to a single font definition, so beyond getting creative and packing variant styles and types in a single font you can't get true font and style mixes. Having said that, you can get pretty far with custom modifiers and shaders.

    Name:  text_bold_alternate.png
Views: 924
Size:  18.3 KB

    The above effect was created with a custom modifier that changed vertex colors for alternating words and used the alpha channel to flag the first letter of each word. A custom shader then used the distance map and the flag to produce either normal or bold characters.

    3) Yes, it works with StringBuilder and I made helper methods to convert ints and floats without generating garbage. I like my memory clean too!

    While namespaces do compile, the Unity editor cannot handle components inside namespaces and refuses to attach them to game objects. I haven't investigated beyond that hurdle.


  10. Posts
    447
    Quote Originally Posted by Jasper Flick View Post
    While namespaces do compile, the Unity editor cannot handle components inside namespaces and refuses to attach them to game objects. I haven't investigated beyond that hurdle.
    I usually get around this by wrapping everything in namespaces and having a component that inherits from those classes. Or, you can wrap everything except the top-most object (the component itself).
    Fluvio 2 - Realtime fluid dynamics in Unity
    Professional | Standard | Enterprise | Evaluation

    Word Game Builder
    Professional | Standard | Enterprise | Evaluation

    Fluid Box | Thinkscroller

    Follow Thinksquirrel:
    Website | Twitter | Facebook | sqr.li


  11. Location
    Amsterdam
    Posts
    301
    It's only good if everything is in the namespace. But we can make do without. I'm currently cleaning up the project and adding docs.


  12. Posts
    2,366
    I'm quite interested in this, but I have to see how the next GUI works. In hindsight, I believe I should have gone with something custom instead of trying to use the Unity GUI, although I wouldn't need anything this fancy.
    My personal game projects -
    Dimension Q Site
    Facebook Page
    Twitter

    Nobody likes rambling gibberish.
    http://whatever.scalzi.com/2006/02/1...rk-at-writing/


  13. Location
    Amsterdam
    Posts
    301
    Waiting for the next Unity GUI might take a while. Use existing solutions if you want to get anything done. But keep in mind that I'm not making an entire GUI system here. I stick to the text box only, but make it a good one.

    Getting editor-mode updates to play nice with prefabs required jumping through a few hoops, but I passed that hurdle. Making stuff production-worthy always requires far more work than you think.


  14. Location
    Thailand
    Posts
    146
    I'm interested in this. I don't mind CCText at all, coming from Objective-C, I'm kinda used to it already


  15. Location
    Amsterdam
    Posts
    301
    Ok I might just use CCText then.

    Another thing I'm on the fence about is how to expose variables. I could either

    1) use public variables, requiring an additional explicit method call to enforce constraints and propagate changes to the mesh;
    2a) use properties disguised as variables, checking constraints and propagating changes immediately on each value change;
    2b) as 2a, but use CamelCase property names according to .NET guidelines, but inconsistent with Unity's approach.

    I'm currently using 2a. Regardless of approach, changes via the inspector propagate immediately.


  16. Location
    Amsterdam
    Posts
    301
    I've decided to include a gradient tool I made into the project. It's convenient. You can either directly access it in code or export it as a texture and use that.

    Name:  text_gradient.png
Views: 722
Size:  20.2 KB

    Here's a shot of the distance map generator. Essential stuff if you want sweet font rendering and special effects. Can also be accessed in code.

    Name:  text_distance_generator.png
Views: 723
Size:  84.4 KB

    This is the font data asset. Not that exciting, but quite important. Imports BMFont-compatible FNT files, both text and XML format.

    Name:  text_font.png
Views: 720
Size:  13.1 KB

    And here's the CCText inspector, showing a prefab instance.

    Name:  text_cctext.png
Views: 718
Size:  22.7 KB

    That's it for version 1.0 features. All that's left now is more polish, documentation, and a demo project.
    Last edited by Jasper Flick; 02-07-2012 at 04:03 AM.


  17. Location
    Netherlands
    Posts
    237


  18. Location
    Lost in Space....................*_*_*_....
    Posts
    213
    If i now use SpriteText to display a Raycast hit info.distance Value in my GUI i get around 8 % and 123 B GC ALLOC in Profiler...
    Can i use your Solution to display this example with less power consumption ?

    Here is a Test Script i use with EZGUI to test the Performance:
    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestText : MonoBehaviour {
    5.    
    6.    public SpriteText text;
    7.    
    8.    public string storyline = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG! 013456789";
    9.      
    10.    private int i = 0;
    11.    private int l = 20;
    12.    private int sl;
    13.    
    14.    void Start() {
    15.       sl = storyline.Length;  
    16.    }
    17.    
    18.    // Update is called once per frame
    19.    void Update () {
    20.       i++;
    21.       l = 20;
    22.      
    23.       if (i >= storyline.Length) i = 0;
    24.       if (sl - i < l) l = sl - i;
    25.       text.Text = storyline.Substring(i,l);
    26.    }
    27. }

    and here is the onGUI Version to test with Unity Text:
    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GUITextTest : MonoBehaviour {
    5.    
    6.    public GUIText text;
    7.    
    8.    public string storyline = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG! 013456789";
    9.      
    10.    private int i = 0;
    11.    private int l = 20;
    12.    private int sl;
    13.    
    14.    void Start() {
    15.       sl = storyline.Length;  
    16.    }
    17.    
    18.    // Update is called once per frame
    19.    void Update () {
    20.       i++;
    21.       l = 20;
    22.      
    23.       if (i >= storyline.Length) i = 0;
    24.       if (sl - i < l) l = sl - i;
    25.       text.text = storyline.Substring(i,l);
    26.    }
    27. }
    Watch profiler and if this is better i will buy your solution directly :-)
    Life's What You Make It - don't forget it's YOU


  19. Location
    Amsterdam
    Posts
    301
    Ok, I'm looking at the profiler line for GUITextTest.Update() with nothing else but a camera active. I don't have EZGui so can't test that.

    Running your GUIText version, I get up to 54B memory allocation and it claims around 7%. The memory allocation is caused by calling Substring() all the time.

    Using CCText instead with the same Substring() approach makes no difference for memory allocation. However, memory allocation drops to zero when I use a Stringbuilder and fill it each frame by iterating through the storyline. It currently takes between 10% and 20%, I haven't deeply profiled an optimized it yet.

    So, is it better? For memory, definitely. For CPU, it's currently a bit worse.
    Last edited by Jasper Flick; 02-07-2012 at 03:02 PM.