Unity实现文本转贴图
作者:小鸟霸哥 时间:2022-05-10 19:53:04
本文实例为大家分享了Unity实现文本转贴图的具体代码,供大家参考,具体内容如下
导入字体
导入ttf字体,修改Character为Custom set,并填入Custom Chars:
可以看到,Unity为我们生成了对应的材质和贴图:
从上图可以看出:
1、Unity中Texture2D的坐标原点为左下角,和OpenGL相同,V坐标与DX相反。
2、某些字符被上下翻转,某些字符被顺时针旋转了90度
这两点需要特别注意。
原理分析
本文中使用的方法是创建一个Texture,然后利用Texture2D的
public Color[] GetPixels(int x, int y, int blockWidth, int blockHeight);
成员方法,读取字体贴图中的像素信息,然后基于特定字符,利用Texture2D的
public void SetPixel(int x, int y, Color color);
方法,将像素信息写入创建的Texrue。
确定GetPixels的参数x,y时,需要注意以下两点:
1、对于被上下翻转的字符,比如数字“1”,利用CharacterInfo. uvTopLeft计算;
2、对于被顺时针旋转90度的字符,比如字母“K”,利用CharacterInfo.uvBottomRight计算。
代码实现
public Texture2D TextToTexture(
Font font,
string text,
int textureWidth, int textureHeight,
int drawOffsetX, int drawOffsetY,
int textGap, int spaceGap, int rowHeight,
Color textColor,
Color backgroundColor)
{
// 创建返回的Texture
var textTexture = new Texture2D(textureWidth, textureHeight, TextureFormat.ARGB32, true);
Color[] emptyColor = new Color[textureWidth * textureHeight];
for (int i = 0; i < emptyColor.Length; i++)
{
emptyColor[i] = backgroundColor;
}
textTexture.SetPixels(emptyColor);
// 字体贴图不可读,需要创建一个新的可读的
var fontTexture = (Texture2D)font.material.mainTexture;
var readableFontTexture = new Texture2D(fontTexture.width, fontTexture.height, fontTexture.format, fontTexture.mipmapCount, true);
Graphics.CopyTexture(fontTexture, readableFontTexture);
// 调整偏移量
var originalDrawOffsetX = drawOffsetX;// 记录一下,换行用
drawOffsetY = textureHeight - drawOffsetY - rowHeight;// 从上方开始画
// 逐个字符绘制
foreach (var @char in text.ToCharArray())
{
if (@char == ' ')
{
drawOffsetX += spaceGap;
continue;
}
if (@char == '\n')
{
// 换行
drawOffsetX = originalDrawOffsetX;
drawOffsetY -= rowHeight;
continue;
}
int charWidth, charHeight;// 字符宽高
Color[] charColor;// 字符颜色,数组内颜色的顺序为从左至右,从下至上
font.GetCharacterInfo(@char, out CharacterInfo info);
if (info.uvTopLeft.x < info.uvBottomRight.x)// 处理被垂直翻转的字符
{
charWidth = info.glyphWidth;
charHeight = info.glyphHeight;
charColor = readableFontTexture.GetPixels(
(int)(readableFontTexture.width * info.uvTopLeft.x),
(int)(readableFontTexture.height * info.uvTopLeft.y),
charWidth, charHeight);
for (int j = 0; j < charHeight; j++)
{
for (int i = 0; i < charWidth; i++)
{
if (charColor[j * charWidth + i].a != 0)
{
textTexture.SetPixel(
drawOffsetX + i,
drawOffsetY + charHeight - j,// 从上往下画,把字符颠倒过来
textColor);
}
}
}
}
else// 处理被顺时针旋转90度的字符
{
charWidth = info.glyphHeight;
charHeight = info.glyphWidth;
charColor = readableFontTexture.GetPixels(
(int)(readableFontTexture.width * info.uvBottomRight.x),
(int)(readableFontTexture.height * info.uvBottomRight.y),
charWidth, charHeight);
for (int j = 0; j < charHeight; j++)
{
for (int i = 0; i < charWidth; i++)
{
if (charColor[j * charWidth + i].a != 0)
{
// 旋转
textTexture.SetPixel(
drawOffsetX + charHeight - j,
drawOffsetY + i,
textColor);
}
}
}
}
// 更新偏移
drawOffsetX += charWidth + textGap;
}
textTexture.Apply();
return textTexture;
}
来源:https://blog.csdn.net/Jingsongmaru/article/details/116717915
标签:Unity,文本,贴图
0
投稿
猜你喜欢
java实现求两个字符串最长公共子串的方法
2023-04-03 12:15:05
Android编程实现下载时主界面与详细界面一致更新的方法
2023-01-02 10:07:35
Java使用iTextPDF生成PDF文件的实现方法
2023-10-15 10:44:13
Java-String类最全汇总(下篇)
2023-11-09 14:45:26
Java实现按权重随机数
2023-11-28 23:15:32
Java实战之在线寄查快递系统的实现
2023-08-10 21:52:23
Android O添加桌面快捷方式的示例
2022-12-27 07:53:19
flutter图片组件核心类源码解析
2023-09-14 16:29:00
c# 遍历获取所有文件的示例代码
2022-11-21 20:01:43
C#调用VB进行简繁转换的方法
2023-02-25 23:19:43
每日六道java新手入门面试题,通往自由的道路--多线程
2022-12-13 16:04:07
Java聊天室之实现一个服务器与多个客户端通信
2021-06-03 11:34:45
浅谈Spring自定义注解从入门到精通
2023-11-25 03:59:12
IntelliJ IDEA 常用设置(配置)吐血整理(首次安装必需)
2021-06-24 15:23:49
简单实现Android本地音乐播放器
2021-09-04 19:28:28
C#构建树形结构数据(全部构建,查找构建)
2022-07-22 12:22:52
使用Spring Cache设置缓存条件操作
2023-01-25 16:38:06
史上最全图文讲解Java泛型
2022-08-23 20:27:47
C#画图之饼图折线图的实现方法
2021-12-05 22:26:13
JavaWeb实现用户登录与注册功能
2022-08-30 17:23:48