Unity 텍스트 스티커 구현

본 논문 의 사례 는 유 니 티 가 텍스트 스티커 를 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
글꼴 가 져 오기
ttf 글꼴 을 가 져 오고,Character 를 Custom set 로 수정 하고,Custom Chars 를 입력 하 십시오:

유 니 티 가 우리 에 게 해당 하 는 소재 와 스티커 를 만들어 준 것 을 볼 수 있다.


위의 그림 에서 볼 수 있다.
1.Unity 에서 Texture2D 의 좌 표 는 원점 이 왼쪽 아래 이 고 OpenGL 과 같 으 며 V 좌 표 는 DX 와 반대 이다.
2.일부 문 자 는 위아래 로 뒤 집 히 고 일부 문 자 는 시계 방향 으로 90 도 회전 합 니 다.
이 두 가 지 는 특별히 주의해 야 한다.
원리 분석
본문 에서 사용 하 는 방법 은 Texture 를 만 든 다음 Texture2D 를 이용 하 는 것 이다.

public Color[] GetPixels(int x, int y, int blockWidth, int blockHeight);
구성원 방법,글꼴 스티커 의 픽 셀 정 보 를 읽 은 다음 특정 문 자 를 기반 으로 Texture2D 를 이용 한

public void SetPixel(int x, int y, Color color);
만 든 Texrue 에 픽 셀 정 보 를 기록 하 는 방법 입 니 다.
GetPixels 의 인자 x,y 를 확인 할 때 다음 두 가 지 를 주의해 야 합 니 다.
1.위아래 로 뒤 집 힌 문자,예 를 들 어 숫자"1"에 대해 CharacterInfo.uvTopLeft 로 계산 합 니 다.
2.시계 방향 으로 90 도 회전 하 는 문자,예 를 들 어 알파벳'K'에 대해 CharacterInfo.uv BottomRight 로 계산한다.
코드 구현

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)
    {
        //      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);

        //        ,           
        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 == '
') { // drawOffsetX = originalDrawOffsetX; drawOffsetY -= rowHeight; continue; } int charWidth, charHeight;// Color[] charColor;// , , 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; }
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기