Unity實現(xiàn)文本轉貼圖
本文實例為大家分享了Unity實現(xiàn)文本轉貼圖的具體代碼,供大家參考,具體內容如下
導入字體
導入ttf字體,修改Character為Custom set,并填入Custom Chars:

可以看到,Unity為我們生成了對應的材質和貼圖:


從上圖可以看出:
1、Unity中Texture2D的坐標原點為左下角,和OpenGL相同,V坐標與DX相反。
2、某些字符被上下翻轉,某些字符被順時針旋轉了90度
這兩點需要特別注意。
原理分析
本文中使用的方法是創(chuàng)建一個Texture,然后利用Texture2D的
public Color[] GetPixels(int x, int y, int blockWidth, int blockHeight);
成員方法,讀取字體貼圖中的像素信息,然后基于特定字符,利用Texture2D的
public void SetPixel(int x, int y, Color color);
方法,將像素信息寫入創(chuàng)建的Texrue。
確定GetPixels的參數(shù)x,y時,需要注意以下兩點:
1、對于被上下翻轉的字符,比如數(shù)字“1”,利用CharacterInfo. uvTopLeft計算;
2、對于被順時針旋轉90度的字符,比如字母“K”,利用CharacterInfo.uvBottomRight計算。
代碼實現(xiàn)
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)
{
// 創(chuàng)建返回的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);
// 字體貼圖不可讀,需要創(chuàng)建一個新的可讀的
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;// 字符顏色,數(shù)組內顏色的順序為從左至右,從下至上
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;
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#.net編程創(chuàng)建Access文件和Excel文件的方法詳解
這篇文章主要介紹了C#.net編程創(chuàng)建Access文件和Excel文件的方法,結合實例形式總結分析了C#創(chuàng)建Access與Excel文件的幾種常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06
通過C#實現(xiàn)在Word中插入或刪除分節(jié)符
在Word中,分節(jié)符是一種強大的工具,用于將文檔分成不同的部分,每個部分可以有獨立的頁面設置,如頁邊距、紙張方向、頁眉和頁腳等,本文將介紹如何使用一個免費的.NET庫通過C#實現(xiàn)插入或刪除Word分節(jié)符,需要的朋友可以參考下2024-08-08

