您好,登錄后才能下訂單哦!
使用Unity怎么將文本轉(zhuǎn)換為貼圖?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
導(dǎo)入字體
導(dǎo)入ttf字體,修改Character為Custom set,并填入Custom Chars:
可以看到,Unity為我們生成了對(duì)應(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、對(duì)于被上下翻轉(zhuǎn)的字符,比如數(shù)字“1”,利用CharacterInfo. uvTopLeft計(jì)算;
2、對(duì)于被順時(shí)針旋轉(zhuǎn)90度的字符,比如字母“K”,利用CharacterInfo.uvBottomRight計(jì)算。
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); // 字體貼圖不可讀,需要?jiǎng)?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)容,你們掌握使用Unity怎么將文本轉(zhuǎn)換為貼圖的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。