纳金网

标题: Sprite Manager与位图字体的结合使用 [打印本页]

作者: 会飞的鱼    时间: 2011-11-5 11:34
标题: Sprite Manager与位图字体的结合使用


           非常有启发作用的一个思路,可以用来动态生成文字,并可以用来代码来控制字体颜色等属性,非常方便快捷!下面是原文:
           

            I have been playing around with Sprite Manager for Unity, which allows you to draw all your 2D sprites in one draw call. I wanted to be able to draw pixel perfect text using Sprite Manager and since I was unable to find any example of this, I wrote my own implementation.
         

           Click on the image below to see the text rendered inside the Unity Web Player.
         

            
           






           I have made a small Unity package that contains the script for my bitmap font loader and the example scene shown above. All scripts in the package except SpriteManager.cs are released into the Public Domain.
         

           Download BitmapFontDemo.unitypackage
         

           Generation of bitmap font
           

           In order for Sprite Manager to be able to render text, it has to be included in a texture. For generating the font texture I used BMFontGen developed for XNA. BMFontGen converts a TrueType or OpenType font into a bitmap (PNG) along with a XML descriptor file.
         

           The BitmapFontDemo package includes a parser for the XML descriptor file that can be used with Sprite Manager. I use my own Texture Atlas Generator to combine the two font textures used in the package into one texture.
         

           When importing the font texture it is important to change “Filter Mode” to “Point” and disable “Generate Mip Maps” by setting “Texture Type” to “Advanced”. Use the shader “Particles/Alpha Blended” for the material associated with the texture.
         

           Pixel perfect orthographic projection
           

           To get pixel perfect rendering of the font (or any 2D for that matter) you have to setup the orthographic projection very precisely. To make this easy in the example scene included in the Unity package above, I have made a script that can be added to the camera and it will automatically setup the orthographic projection.
         

           public class Orthographic : MonoBehaviour
         

           {
         

             float offset = 0.0f;
         

             void Start()
         

             {
         

               if(SystemInfo.graphicsDeviceVersion.ToLower().StartsWith("direct3d 9"))
         

               {
         

                 offset = 0.5f;
         

               }
         

               camera.orthographic = true;
         

             }
         



             void Update()
         

             {
         

               camera.orthographicSize = Screen.height/2;
         

               camera.transform.position = new Vector3(Screen.width/2 - offset,
         

                                           Screen.height/2 - offset, -1);
         

             }
         

           }
         

           Note that the camera is offset by a half-pixel when using Direct3D 9 on Windows, further details on why this is necessary here.
         



           Update
         




           The screenshot above shows that you can also change the color of the individual characters using Sprite Manager. I got the result above by changing the following (long) line of code in BitmapFontDemo.AddText
         

           spriteManager.AddSprite(go, s.texCoords.width, s.texCoords.height,
         

             (int)s.texCoords.x,  (int)s.texCoords.y + (int)s.texCoords.height,
         

             (int)s.texCoords.width, (int)s.texCoords.height, false);
         

            to
         

            Sprite sp = spriteManager.AddSprite(go, s.texCoords.width, s.texCoords.height,
         

             (int)s.texCoords.x,(int)s.texCoords.y + (int)s.texCoords.height,
         

             (int)s.texCoords.width, (int)s.texCoords.height, false);
         

           sp.SetColor(new Color(Random.Range(0.2f, 1.0f), Random.Range(0.2f, 1.0f),
         

             Random.Range(0.2f, 1.0f)));
         

           Update 2
         

           Another minor update showing how to draw dynamic text that is updated every frame. Click on the image below to see it in action in the Unity Web Player.
           






作者: tc    时间: 2012-2-4 23:26
好可爱的字,学习了

作者: tc    时间: 2012-3-25 23:18
不错不错,收藏了

作者: C.R.CAN    时间: 2012-4-30 23:27
呵呵,很漂亮啊

作者: C.R.CAN    时间: 2012-5-2 23:22
水。。。

作者: markq    时间: 2012-5-4 23:23
  谢谢分享



爱生活 爱3D 爱纳金网



www.narkii.com
作者: C.R.CAN    时间: 2012-5-27 23:26
非常感谢,管理员设置了需要对新回复进行审核,您的帖子通过审核后将被显示出来,现在将转入主题

作者: 晃晃    时间: 2012-6-22 23:20
很经典,很实用,学习了!

作者: 菜刀吻电线    时间: 2012-9-10 23:25
好铁多多发,感激分享

作者: 奇    时间: 2012-10-1 23:19
有意思!学习了!

作者: tc    时间: 2012-10-14 23:26
非常感谢,管理员设置了需要对新回复进行审核,您的帖子通过审核后将被显示出来,现在将转入主题

作者: 奇    时间: 2013-1-25 23:26
很经典,很实用,学习了!

作者: 奇    时间: 2013-2-13 23:24
不会吧,太恐怖了





欢迎光临 纳金网 (http://old.narkii.com/club/) Powered by Discuz! X2.5