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;
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
photonnetwork.instantiate에서 gamepobject 유형을 생성 한 다음 상태 및 값을 참조하는 방법주로 마지막 기사에서 일어난 일의 수정입니다. 지난번↓ 그럼 주제입니다. (타이틀이 정리되어 없어서 죄송합니다) 우선 전회의 Illegal view ID:0입니다만 photonnetwork.instantiate를 사...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.