12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 6608|回复: 13
打印 上一主题 下一主题

[经验分享] Sprite Manager与位图字体的结合使用

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2011-11-24 10:03:00 |只看该作者 |倒序浏览


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

            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
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-4-3 13:16:05 |只看该作者
响应天帅号召,顶
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-4-10 23:18:16 |只看该作者
凡系斑竹滴话要听;凡系朋友滴帖要顶
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

地板
发表于 2012-5-19 23:21:31 |只看该作者
先垫一块,再说鸟
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-7-22 23:24:45 |只看该作者
凡系斑竹滴话要听;凡系朋友滴帖要顶
回复

使用道具 举报

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

6#
发表于 2012-7-24 09:35:05 |只看该作者
高手速来赐教!kismet里面怎么加一个延迟DELAY??



unity特效大家PP_武功盖世呀








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






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








antares_universe(vizio)可视化编辑
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-8-13 08:49:20 |只看该作者
我看看就走,你们聊!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

8#
发表于 2012-10-27 23:24:24 |只看该作者
其实楼主所说的这些,俺支很少用!
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-10-29 23:25:34 |只看该作者
俺是新人,这厢有礼了!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

10#
发表于 2012-10-31 23:24:03 |只看该作者
读铁系缘分,顶铁系友情
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-6-7 06:33 , Processed in 0.097921 second(s), 27 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部