Unity實現(xiàn)文本轉(zhuǎn)貼圖
本文實例為大家分享了Unity實現(xiàn)文本轉(zhuǎn)貼圖的具體代碼,供大家參考,具體內(nèi)容如下
導入字體
導入ttf字體,修改Character為Custom set,并填入Custom Chars:
可以看到,Unity為我們生成了對應的材質(zhì)和貼圖:
從上圖可以看出:
1、Unity中Texture2D的坐標原點為左下角,和OpenGL相同,V坐標與DX相反。
2、某些字符被上下翻轉(zhuǎn),某些字符被順時針旋轉(zhuǎn)了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、對于被上下翻轉(zhuǎn)的字符,比如數(shù)字“1”,利用CharacterInfo. uvTopLeft計算;
2、對于被順時針旋轉(zhuǎn)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); // 調(diào)整偏移量 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ù)組內(nèi)顏色的順序為從左至右,從下至上 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// 處理被順時針旋轉(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#優(yōu)化if...else代碼的方案總結(jié)
在編寫代碼實現(xiàn)業(yè)務需求過程中,會使用到大量的if...else 判斷語句,隨業(yè)務復雜程度不同,導致判斷語句出現(xiàn)多層嵌套、多分支等情況,導致代碼可讀性變差、增加維護難度,本文介紹了C# 如何優(yōu)化 if...else 讓代碼優(yōu)雅起來,需要的朋友可以參考下2024-06-06C#數(shù)據(jù)表格(DataGridView)控件的應用案例
這篇文章主要介紹了C#數(shù)據(jù)表格(DataGridView)控件的應用案例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03Avalonia封裝實現(xiàn)指定組件允許拖動的工具類
這篇文章主要為大家詳細介紹了Avalonia如何封裝實現(xiàn)指定組件允許拖動的工具類,文中的示例代碼講解詳細,感興趣的小伙伴快跟隨小編一起來學習學習吧2023-03-03.net C# 實現(xiàn)任意List的笛卡爾乘積算法代碼
笛卡爾(Descartes)乘積又叫直積。假設集合A={a,b},集合B={0,1,2},則兩個集合的笛卡爾積為{(a,0),(a,1),(a,2),(b,0),(b,1), (b,2)}。2013-05-05C#中AutoResetEvent控制線程用法小結(jié)
本文主要來自一道面試題,由于之前對AutoResetEvent的概念比較模糊,面試題題目很簡潔:兩個線程交替打印0~100的奇偶數(shù),你可以先動手試試,我主要是嘗試在一個方法里面完成這個任務,需要的朋友可以參考下2022-07-07