Search Unity

Font Texture Question

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

  1. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    File.PleaseWriteAtLeastSomeOfTheBytesIfYouFeelLikeIt? :D Hmm, probably you could loop though the array and write one byte at a time instead? I think...haven't tried it.

    --Eric
     
  2. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    That method doesn't appear in the Mono docs - are you sure of the spelling?

    :p
     
  3. mattsonic

    mattsonic

    Joined:
    Oct 3, 2008
    Posts:
    6
    For iPhone folks who want to use this script, here's a version that works with earlier versions of Mono.

    Code (csharp):
    1.  
    2. import System.IO;
    3.  
    4. @MenuItem ("Assets/Save Font Texture")
    5.  
    6. static function SaveFontTexture () {
    7.     var tex = Selection.activeObject as Texture2D;
    8.     if (tex == null) {
    9.         EditorUtility.DisplayDialog("No texture selected", "Please select a texture", "Cancel");
    10.         return;
    11.     }
    12.     if (tex.format != TextureFormat.Alpha8) {
    13.         EditorUtility.DisplayDialog("Wrong format", "Texture must be in uncompressed Alpha8 format", "Cancel");
    14.         return;
    15.     }
    16.  
    17.     // Convert Alpha8 texture to ARGB32 texture so it can be saved as a PNG
    18.     var texPixels = tex.GetPixels();
    19.     var tex2 = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);
    20.     tex2.SetPixels(texPixels);
    21.  
    22.     // Save texture
    23.     var texBytes = tex2.EncodeToPNG();
    24.     var fileName = EditorUtility.SaveFilePanel("Save font texture", "", "font Texture", "png");
    25.     if (fileName.Length > 0) {
    26.       var f : FileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
    27.       var b : BinaryWriter = new BinaryWriter(f);
    28.       for (var i = 0; i < texBytes.Length; i++) b.Write(texBytes[i]);
    29.       b.Close();
    30.     }
    31.  
    32.     DestroyImmediate(tex2);
    33. }
    34.  
    Thanks so much Eric for this fantastic script! :)
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Very nice; you should add that to the wiki.

    --Eric
     
  5. marty

    marty

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

    And once again, thanks Eric!
     
  6. mattsonic

    mattsonic

    Joined:
    Oct 3, 2008
    Posts:
    6
    Added to the wiki :)
     
  7. zoul

    zoul

    Joined:
    May 5, 2007
    Posts:
    50
    I just ran across this thread while looking for a way to have customized fonts in my game, and have a quick question.

    I used the 'Saved Font Texture' script (the iPhone version posted most recently), and then opened my saved PNG's in photoshop. I applied a simple Stroke and Color Overlay to them, and saved them out as PSD's and deleted the PNG's.

    I created the custom font material using the Textured Font shader from the Wiki, and am now looking at my fonts in the game preview and they are ... rather poorly rendered.

    The font by itself looks fine in Unity as is, I can change the color of the font by creating a custom material and changing the 'text color' in it. In this case, I added the color directly to the texture, so my 'Text Color' is just set to white, although my font appears yellow on screen. The outline is the part that concerns me, because it displays very poorly.

    Any suggestions on how I can make this a bit crisper?
     
  8. mattsonic

    mattsonic

    Joined:
    Oct 3, 2008
    Posts:
    6
    Zoul - I don't know if this is the *best* approach, but this is what I did:

    1) Bump up your texture size inside of Photoshop. I scaled my original 256x256 texture to 512x512.
    2) Create a custom alpha channel inside of Photoshop instead of relying on the alpha produced by Unity.
    3) Turn off mip map creation inside of your texture import settings. (This will greatly improve the UI on the iPhone, but not in the editor).

    The biggest help IMO is disabling the mip maps. After taking these steps, my GUI fonts look very crisp.

    Hope this helps!

    Matt
     
  9. zoul

    zoul

    Joined:
    May 5, 2007
    Posts:
    50
    What do you mean, bump up the point size of the font? If I do that, then my font displays at an incorrect size for my UI. I am exported the same font three times, at different font sizes.

    The first texture is 128x128, and the second is 256x256 while the third (largest font, at like 70 point) is 512x256 (why it is not power of two, I don't know).

    When I try to swap out the 128x128 with the 256x256 the font displays completely wrong ... so, apparently, it has to use the 128x128 to display the smaller font properly.

    I also tried turning off mip maps, and that did not appear to make much of a difference either.
     
  10. mattsonic

    mattsonic

    Joined:
    Oct 3, 2008
    Posts:
    6
    Hi Zoul,

    I just meant make the point size as large as it can be and still look good for your UI.

    Once you have that, then you can scale up the texture size in Photoshop. In my case, I took a 256x256 texture and scaled it up to 512x512. Now when you apply layer styles, for example, it will be at a higher res. Also, I assume that this greatly reduces the visible artifacts from PVRTC compression.

    Then I manually created an alpha channel in Photoshop.

    When the texture was finished, I went into the Unity import settings for my PSD and turned off mip map creation. This setting made a *huge* difference on the actual iPhone UI, but not in the editor.

    Hope this helps. BTW, I am not an authority on any of this stuff, but these steps worked for my game.

    Matt
     
  11. zoul

    zoul

    Joined:
    May 5, 2007
    Posts:
    50
    I appreciate the prompt feedback, and will take a closer look at pulling this off. I only tried doing this really quick, so I'll try it out again later when I have more time to invest in it.

    Thanks,
     
  12. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Eric5h5 - I know this post is old, but I used your system here and was hoping I could fade a 3D Text in and out - by I cannot for the life of me get any sort of fading with text...any ideas on what I could be missing?

    Let me know what you would need to see and I can send you any info. Thanks.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Set the alpha value of the renderer.material.color as desired.

    --Eric
     
  14. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    I am doing this:

    Code (csharp):
    1.  
    2.     protected override IEnumerator Fade(float startLevel, float endLevel, float time)
    3.     {
    4.         float speed = 1.0f / time;
    5.  
    6.         for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * speed)
    7.         {
    8.             float a = Mathf.Lerp(startLevel, endLevel, t);
    9.             text.font.material.color = new Color(text.font.material.color.r,
    10.                 text.font.material.color.g,
    11.                 text.font.material.color.b, a);
    12.  
    13.             gameObject.transform.position += new Vector3(0,0.01f,0);
    14.  
    15.             yield return null;
    16.         }
    17.     }
    18.  
    Where text is a TextMesh..am I missing something?
     
  15. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Oh dear god I got it - thanks Eric5h5 I was fading the wrong material color.
     
  16. KEMBL

    KEMBL

    Joined:
    Apr 16, 2009
    Posts:
    181
    Hello, All ! :)

    Eric's script still great and I have got one related question:

    Today we on Unity3D 3.3.0 and I wonder if some one knows, is possible to have access to char distribution information across font texture?

    For example, I have some font and I need to know coordinates of char "W" on its texture. Without this simple information still impossible to build RichEdit control ( http://msdn.microsoft.com/en-us/library/bb787877(v=vs.85).aspx ) or multi-platform HTML render for example.
     
  17. shchvova

    shchvova

    Joined:
    Mar 19, 2012
    Posts:
    9
    So, feel a little slow with the question, but is there a way to use generated font in OnGUI calls? I just getting nothing drawn with usage of generated custom font, is there any suggestions? (It works great with GUIText)

    UPD. If I remove Per Character Kerning, string appears, but it ignoring texture and shader I set to it :(
     
    Last edited: Mar 19, 2012