纳金网

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

作者: 驰骋的风    时间: 2011-11-24 10:03
标题: 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 ***eType 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 = ***e;
         

             }
         

            
         

             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.
         


           
         


            Download BitmapFontDemo2.unitypackage
           


           本文转自:Lu kasz.dk
         

作者: 奇    时间: 2012-4-3 13:16
响应天帅号召,顶

作者: 奇    时间: 2012-4-10 23:18
凡系斑竹滴话要听;凡系朋友滴帖要顶

作者: 晃晃    时间: 2012-5-19 23:21
先垫一块,再说鸟

作者: C.R.CAN    时间: 2012-7-22 23:24
凡系斑竹滴话要听;凡系朋友滴帖要顶

作者: 驰骋的风    时间: 2012-7-24 09:35
高手速来赐教!kismet里面怎么加一个延迟DELAY??



unity特效大家PP_武功盖世呀








【严重分享】Unity3D_3.0破解版[安装包][下载]






高手请赐教!用模型做的特效动画材质丢失








antares_universe(vizio)可视化编辑

作者: C.R.CAN    时间: 2012-8-13 08:49
我看看就走,你们聊!

作者: 晃晃    时间: 2012-10-27 23:24
其实楼主所说的这些,俺支很少用!

作者: C.R.CAN    时间: 2012-10-29 23:25
俺是新人,这厢有礼了!

作者: 晃晃    时间: 2012-10-31 23:24
读铁系缘分,顶铁系友情

作者: tc    时间: 2013-2-10 23:26
读铁系缘分,顶铁系友情

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

作者: 奇    时间: 2013-3-15 23:30
不错不错,收藏了

作者: nts    时间: 2013-10-17 11:01
不错的教程哦




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