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

可以看到,Unity為我們生成了對應(yīng)的材質(zhì)和貼圖:


從上圖可以看出:
1、Unity中Texture2D的坐標(biāo)原點(diǎn)為左下角,和OpenGL相同,V坐標(biāo)與DX相反。
2、某些字符被上下翻轉(zhuǎn),某些字符被順時(shí)針旋轉(zhuǎn)了90度
這兩點(diǎn)需要特別注意。
原理分析
本文中使用的方法是創(chuàng)建一個(gè)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時(shí),需要注意以下兩點(diǎn):
1、對于被上下翻轉(zhuǎn)的字符,比如數(shù)字“1”,利用CharacterInfo. uvTopLeft計(jì)算;
2、對于被順時(shí)針旋轉(zhuǎn)90度的字符,比如字母“K”,利用CharacterInfo.uvBottomRight計(jì)算。
代碼實(shí)現(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)建一個(gè)新的可讀的
var fontTexture = (Texture2D)font.material.mainTexture;
var readableFontTexture = new Texture2D(fontTexture.width, fontTexture.height, fontTexture.format, fontTexture.mipmapCount, true);
Graphics.CopyTexture(fontTexture, readableFontTexture);
// 調(diào)整偏移量
var originalDrawOffsetX = drawOffsetX;// 記錄一下,換行用
drawOffsetY = textureHeight - drawOffsetY - rowHeight;// 從上方開始畫
// 逐個(gè)字符繪制
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ù)組內(nèi)顏色的順序?yàn)閺淖笾劣遥瑥南轮辽?
font.GetCharacterInfo(@char, out CharacterInfo info);
if (info.uvTopLeft.x < info.uvBottomRight.x)// 處理被垂直翻轉(zhuǎn)的字符
{
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// 處理被順時(shí)針旋轉(zhuǎn)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)
{
// 旋轉(zhuǎn)
textTexture.SetPixel(
drawOffsetX + charHeight - j,
drawOffsetY + i,
textColor);
}
}
}
}
// 更新偏移
drawOffsetX += charWidth + textGap;
}
textTexture.Apply();
return textTexture;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 實(shí)現(xiàn)拖拉控件改變位置與大小的方法
下面小編就為大家分享一篇C# 實(shí)現(xiàn)拖拉控件改變位置與大小的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
winform實(shí)現(xiàn)限制及解除鼠標(biāo)移動范圍的方法
這篇文章主要介紹了winform實(shí)現(xiàn)限制及解除鼠標(biāo)移動范圍的方法,涉及C#控制WinForm鼠標(biāo)事件屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
C#優(yōu)化if...else代碼的方案總結(jié)
在編寫代碼實(shí)現(xiàn)業(yè)務(wù)需求過程中,會使用到大量的if...else 判斷語句,隨業(yè)務(wù)復(fù)雜程度不同,導(dǎo)致判斷語句出現(xiàn)多層嵌套、多分支等情況,導(dǎo)致代碼可讀性變差、增加維護(hù)難度,本文介紹了C# 如何優(yōu)化 if...else 讓代碼優(yōu)雅起來,需要的朋友可以參考下2024-06-06
C#數(shù)據(jù)表格(DataGridView)控件的應(yīng)用案例
這篇文章主要介紹了C#數(shù)據(jù)表格(DataGridView)控件的應(yīng)用案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Avalonia封裝實(shí)現(xiàn)指定組件允許拖動的工具類
這篇文章主要為大家詳細(xì)介紹了Avalonia如何封裝實(shí)現(xiàn)指定組件允許拖動的工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起來學(xué)習(xí)學(xué)習(xí)吧2023-03-03
.net C# 實(shí)現(xiàn)任意List的笛卡爾乘積算法代碼
笛卡爾(Descartes)乘積又叫直積。假設(shè)集合A={a,b},集合B={0,1,2},則兩個(gè)集合的笛卡爾積為{(a,0),(a,1),(a,2),(b,0),(b,1), (b,2)}。2013-05-05
C#中AutoResetEvent控制線程用法小結(jié)
本文主要來自一道面試題,由于之前對AutoResetEvent的概念比較模糊,面試題題目很簡潔:兩個(gè)線程交替打印0~100的奇偶數(shù),你可以先動手試試,我主要是嘗試在一個(gè)方法里面完成這個(gè)任務(wù),需要的朋友可以參考下2022-07-07

