Search Unity

Font Texture Question

Discussion in 'Editor & General Support' started by marty, Apr 15, 2007.

  1. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    After I import a TT font into Unity and it automatically generates a texture for the font map, is there a way that I can then edit that texture as a bitmap, say, in Photoshop?

    When I double-click on the texture in the editor, it launches Photoshop and acts like it wants to let me edit it. But then, Photoshop errors out, telling me that it does not recognize the file format.
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Only way I've been able to sorta do this is to assign the font texture to a GUITexture and take a screenshot... then try and crop it perfectly.

    It sure would be nice otherwise. I think I feature requested it and they're aware of it.

    -Jon
     
  3. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I would also appreciate being able to open unity generated textures like that in photoshop.

    Whishlist ++
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Havent tried this - but arent all Unity textures in the DXT format? Theres a plugin for photoshop (at least on PC) that allows you to open the DXT (DDS) file format.

    Cheers
    Shaun
     
  5. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Where do you find the DXT file? I don't know of a way to follow Unity's metadata linking and all that jazz.

    -Jon
     
  6. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    I'm not 100% sure its a DXT, but in theory it should be stored in the _projectname_/library/cache folder.

    I haven't any idea how to find it though - the asset database that Unity creates surely has the linkages, but I don't think we are supposed to directly access this.

    Also - the texture would most likely be overwritten when you build anyway.

    Its probably safe to say we cant do this until OTEE change GUIText to allow an external texture... but I would love to be proven wrong :D

    Cheers
    Shaun
     
  7. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    You can already make your own font texture. Don't remember how exactly but it is possible.
     
  8. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Making fontmaps isn't fun.

    There was a tutorial on the Wiki a while back. It was painful just to read it.

    Since Unity already eats TT fonts and spits out usable fontmaps, the last mile in the process would be being able to take those Unity-made, Unity-friendly maps into PS to doll them up.

    Hopefully, one of the Unitoids is quitely reading this thread while nodding a little smile.
     
  9. aaronsullivan

    aaronsullivan

    Joined:
    Nov 10, 2005
    Posts:
    986
    It might not be fun, but it's ~1000 times faster than waiting for an update to Unity.
    :wink:

    Seriously, though, it's really not that difficult with any graphics app that lets you type text in. It's a little time consuming. I just stayed up late working on one (because it wasn't a Windows TrueType file.) Probably the trickiest part is doing all the per character kerning and making sure the letters have a common baseline. It depends on how picky you are, though.

    As an aside, I'd love to see support for other font formats like FFIL and Mac's font suitcase format (TrueType with resources), but I know that's probably a tall order. What is an FFIL anyway? :D
     
  10. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    You could whip up a program for generating font textures in RealBasic pretty easily.

    And GraphicConverter can read DDS files and convert them, (and pretty much any 2d bitmap format you can think of).
     
  11. aaronsullivan

    aaronsullivan

    Joined:
    Nov 10, 2005
    Posts:
    986
    Fantastic podperson! Thanks for volunteering!

    ;)

    Not really familiar with programming in RealBasic, but if it is easy, and anyone feels confident, this would be a nice little tool for a grateful community. Could it also solve the True Type only problem? Or, no? I'm only assuming there must be an easy way to rasterize standard fonts in RealBasic.

    (Graphic Converter does that, indeed, but the problem of finding the file still remains.)
     
  12. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Seriously.

    You write that RealBasic program, and you'll be my personal hero!
     
  13. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    This little utility can be handy for setting up font textures. I used it to make the ingame font for Phoenix Final. It takes any installed font and turns it into a grid of characters.

    You'll have to fiddle with the settings a bit to get it to generate an even grid rather than a tightly packed arrangement.
     
  14. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I couldn't find the relevant documentation aside from some templates for creating character maps. Also, I don't know if there's a way to embed inter-character spacing into the font information for proportional fonts, so any information you can give me will be helpful.

    With those disclaimers, here's the utility:

    Correct URL
    http://loewald.com/unity/unityfontmapper.zip

    It's a universal binary.

    dx is the offset from the left of the cell
    dy is the offset from the bottom of the cell for the character baseline

    The maps are generated at 1024x1024, but I can easily make that adjustable (it is in the code, but I didn't build a UI for it).

    Note that the character set rendered is editable (fiddly though -- a RealBasic quirk). So you can modify the character set to your heart's content.

    Edit:

    It looks like ncarter's utility is *nicer* but not so convenient for Unity. The utility I wrote was 36 minutes from reading your post to compiled app...
     
  15. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I've got to start playing with RealBASIC one of these days.

    Thanks, all!
     
  16. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I added the ability to set the fontmap size (from 256 to 2048 pixels on a side). The other parameters (e.g. font size and offsets) are scaled as well (based on a 1024x1024 base size).

    Oh and I just fixed a major bug -- it was printing everything one row high and omitting the top row.
     
  17. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I can't download the file.

    Is something wrong?
     
  18. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
  19. aaronsullivan

    aaronsullivan

    Joined:
    Nov 10, 2005
    Posts:
    986
    Impressive! Even a nice icon. :D

    Could have saved me some serious time, and it will in the future. Thanks, so much, for digging right in (Seriously, one hour later: BAM! Do you have a paypal account for donations?)

    I love that you can tweak what letters go where by clicking in that upper left box. Slick.

    The only job after making one of these is figuring out the per character kerning.

    One simple way to do it would be to find the furthermost white pixel on the right of the character's square and then add dx to it (for even spacing) then divide by the size of each square to get your value between 0 and 1. That way you could have a pop up list with all the values... getting that into Unity would still require typing, but all the work would be done... and precise, unless I'm missing something.

    For polished icing, would it be possible to populate the font list with fonts from the user's font folder, too? :)
     
  20. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    If the font is available (at OS level) it's available (i.e. if Illustrator can see it...) -- so it should see user fonts etc.

    I can easily capture character width -- I just don't know what to do with it for Unity's benefit. If there's somewhere I can store it for Unity I'm happy to add that functionality.
     
  21. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Added support for bold and italic variations.
     
  22. aaronsullivan

    aaronsullivan

    Joined:
    Nov 10, 2005
    Posts:
    986
    Hmm... doesn't get my "local" fonts. Not a big issue, I just copied it over to the root library font folder and then it shows up.

    There is a .fontSettings file for each font in the Assets folder, but... it's not very user readable, doesn't look safe to mess with. :)

    Just a window with a list of each non-one character kerning would be a huge time saver.
     
  23. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Oh and I just verified -- it is picking up fonts in my ~/Library/Fonts directory.
     

    Attached Files:

  24. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    nice one - thanks!
     
  25. aaronsullivan

    aaronsullivan

    Joined:
    Nov 10, 2005
    Posts:
    986
    Odd about the font folder thing. Mine simply don't show up unless they are put in the root. I have at least a dozen plus that don't show. They show up fine in Photoshop, for instance.

    I wouldn't want you to worry too much about that though. I'd rather see per character kerning while you're engaged in this. :D

    Serious about this, though (you might have missed it earlier):
    Do you have a paypal account for donations?
     
  26. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    For what it's worth, I'm having the exact same problem as Aaron.

    Talk about your shoddy software. ;-)
     
  27. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    But as far as I know the only way to get Unicode (important for multiple languages support) is to import a .ttf

    -Jon
     
  28. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    It looks to me like Unity is storing some useful ancillary information along with the raw bitmap. Without knowing how this works I'm not really sure how to proceed.

    It would be pretty neat to be able to generate fully customized text, but I'm guessing that you might be better off creating nice font shaders (if you have Pro) or custom fonts.
     
  29. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    My paypal account (which I use mainly to become poorer ;-) ) is tloewald at gmail dot com.

    But I'd really like to do the kerning stuff automatically if I can.
     
  30. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    True!

    A better option would be to use custom shaders (for example if you wanted glowing text) or to be able to grab Unity's bitmap, change it, and reimport it.
     
  31. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I've added the ability to copy or save out a table of character widths.

    Note that this isn't resolution independent -- it's the pixel width of the character (actually 1/10th of the width of ten characters in a row because RealBasic only gives out integer string widths) so it will be more for a 2048x2048 texture than for a 512x512.

    If someone points me to Unity documentation showing how I can make this more automatic (essentially generate Unity fonts directly) I'd be happy to do that.
     
  32. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Heya, podperson!

    Thanks again for your awesome font utility.

    I'm having a problem with it that I've been noodling with since you first uploaded it. Some fonts don't show up in it. And they're always the ones I want to map!

    They are all TT fonts and are all in my root Library folder and are all instaleld in the system and show up in other apps (i.e. Photoshop).

    I'm sure it's more of a MacOS thing than a your app thing. Still, do you, or does anyone else, have any idea what might be up with this?
     
  33. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Heya, Podperson!

    Your Unity Font Mapper is great.

    However, it doesn't list all of the fonts on my system, no matter what I do.

    I made a little font browser in RealBasic myself, using some sample code, and it does lit all of my system fonts.

    Could I possibly send you this font browser I made so that you can figure out how to get you (very cool) Mapper app to see all fonts?

    TIA!
     
  34. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Nearly two years in the making! SaveFontTexture on the wiki saves the generated font texture! Well, OK, I only got around to doing this yesterday, but I thought I'd post in this almost two-year-old topic in case someone does a search sometime and comes up with this.

    --Eric
     
  35. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Thanks, Eric!

    But I'm getting a script error: "WriteAllBytes is not a member of System.IO.File".

    I tried adding "import System;" to the top of the script, but that didn't help.
     
  36. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Hmm, works here...you're using Unity 2.1? Doing an import won't help, because writing "System.IO.File.WriteAllBytes" is the same thing as doing "import System.IO" followed by "File.WriteAllBytes" later. Since I'm just using the System.IO namespace once in the script, there wasn't much point adding an import line.

    --Eric
     
  37. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I'm using Unity iPhone 1.0.1 and it doesn't work there.

    I tried it in 2.x though and it works!

    So, I can take this texture, modify it in Photoshop then bring the texture back into my project in place of the original texture and have the modified one?! Wow!

    Thanks, Eric!
     
  38. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I'm using Unity iPhone 1.0.1 and it doesn't work there.

    I tried it in 2.x though and it works!

    So, I can take this texture, modify it in Photoshop then bring the texture back into my project in place of the original texture and have the modified one?! Wow!

    Thanks, Eric!
     
  39. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can't actually replace the generated texture, but you can make a new font material with the new texture (preferably using the textured font shader on the wiki) and use that instead of the generated font material.

    --Eric
     
  40. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I hate to ask, Erci, but could you maybe give me a quick set of steps on this?

    I've tried building it up from a custom font, but the characters seem to be offset wrong.

    TIA!

    And once again, thanks for this awesome script!
     
  41. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Make sure you're using the correct font on your GUIText. Setting the material isn't enough; it needs the font for the letter spacing info.

    --Eric
     
  42. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Dangit!

    I just can't seem to get this to work, Eric. And I want to soooo badly! ;-)

    I've attached a ZIP of a teeny little project where I've taken a source font and made a new font with a customized texture.

    Any chance you could take a quick look and let me know what I'm doing wrong here?

    TIA!
     

    Attached Files:

  43. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    On the "guitext_new" object, drag the source_font .ttf font onto the Font slot, replacing new_font, which you can get rid of. You don't need to create a custom font, unless you want to build a bitmap font from scratch and input all the kerning values. (Yes, it's all slightly confusing. ;) )

    --Eric
     
  44. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Awesome, Eric!

    Awesome, awesome, awesome!

    Oh, and thanks! ;-)
     
  45. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    For the benefit of the one person out there in the world (theoretically) who is as slow-witted as me, here is what you need to do to use Eric's awesome script, stepwise ...

    1. Get the TrueType font that you want to modify in Unity at the desired Font Size (so that you can use Pixel Correct in the GUI_Text object that will use it for crisp rendering).

    2. Use Eric's Save Font Texture Editor script (already installed as described in this post) to save the texture Unity generates automatically for the font.

    3. Edit that texture file in Photoshop to use in your project.

    4. Create a new Material in Unity.

    5. Set the texture of the material to the modified PNG texture.

    6. Set the shader of the material to the Textured Text Shader texture (available on the Wiki).

    7. In any scene GUI_Text objects that will use the custom font, apply the original font to the object then change its material to the custom material created in steps 4-6.
     
  46. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Cool, you should add that to the wiki entry. Although in step 3, you don't need to save a flattened PNG version; a PSD is fine, keeping in mind the usual caveats about how to best handle alpha channels.

    Edit: oops, I see you edited step 3. Um, nevermind. :)

    --Eric
     
  47. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Done and done!
     
  48. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Heya, Eric. Any idea why the script is crapping out (i.e. not compiling) on Unity iPhone?

    I've attached a screenshot of the error message.

    It's kind of puzzling - I checked the Mono docs and the WriteAllBytes call seems copacetic, which is no surprise since it works on Unity 2.x, after all.

    If you don't have any idea, let me know and I'll report it as a bug.
     

    Attached Files:

  49. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I believe it's because WriteAllBytes is limited to .net 2.0 or later, which isn't in the iPhone version for space reasons.

    --Eric
     
  50. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Makes sense to me. ;-)

    Is there a substitute for WriteAllBytes that might work in Unity iPhone?