找回密码
 立即注册
查看: 6752|回复: 81

[脚本] Unity代码控制动态改变字体颜色

[复制链接]
发表于 2012-12-21 19:15 | 显示全部楼层 |阅读模式

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

[/url]

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.

[url=http://lukasz.dk/files/BitmapFontDemo.unitypackage]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.

[url=http://lukasz.dk/2011/05/01/pixel-perfect-bitmap-font-in-unity/#2][/url]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

评分

参与人数 1鲜花 +1 收起 理由
kukupapa + 1 神马都是浮云

查看全部评分

发表于 2013-8-7 18:27 | 显示全部楼层
感谢楼主的无私分享!
发表于 2017-2-9 21:05 | 显示全部楼层
很不错
发表于 2017-2-9 20:58 | 显示全部楼层
顶顶多好
发表于 2017-2-9 20:38 | 显示全部楼层
难得一见的好帖
发表于 2017-2-9 20:13 | 显示全部楼层
说的非常好
发表于 2017-2-9 20:41 | 显示全部楼层
LZ真是人才
发表于 2017-2-28 17:22 | 显示全部楼层
很不错
发表于 2017-2-28 17:10 | 显示全部楼层
楼主是超人
发表于 2017-2-28 18:03 | 显示全部楼层
好帖就是要顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-22 07:20 , Processed in 0.104872 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表