最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity實(shí)現(xiàn)文本轉(zhuǎn)貼圖

 更新時(shí)間:2021年05月14日 15:06:00   作者:小鳥霸哥  
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)文本轉(zhuǎn)貼圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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);

        // 字體貼圖不可讀,需要?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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

洱源县| 镇江市| 探索| 凤冈县| 云林县| 勐海县| 凤台县| 江孜县| 饶平县| 万载县| 龙门县| 南宫市| 平泉县| 永嘉县| 康平县| 铁岭县| 香港| 榆树市| 商河县| 新野县| 安塞县| 荔波县| 洪江市| 长武县| 赣州市| 虹口区| 清涧县| 大新县| 罗定市| 吕梁市| 资中县| 孟津县| 荆门市| 海城市| 龙州县| 三江| 剑川县| 招远市| 富民县| 松江区| 阜阳市|