블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2737)
Unity3D (817)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
04-25 09:56

프로젝트는 NGUI를 사용하며 최근에 로딩 인터페이스의 프롬프트 텍스트가 깨지는 버그가 발생했습니다.


다음 기사를 참조했습니다.

http://blog.csdn.net/langresser_king/article/details/22095235

이 문제가 발생하는 이유를 먼저 이해합시다.



이해해야 할 것은 NGUI의 UILabel은 Unity에서 제공하는 글꼴을 글꼴의 입력으로 사용한다는 것입니다.

Unity는 글꼴 생성에 대해 매우 경제적이며 이는 정상적인 엔진이며 엔진은 이와 같아야 합니다.



(1) Loading 인터페이스의 UILabel에 세 단어 로딩을 표시하면 Unity가 이 세 단어의 텍스처를 생성하고 128x128의 FontTexture가 충분해야 합니다. 그런 다음 UILabel은 글꼴에 따라 각 단어의 그래픽을 가져옵니다.



(2) 그런 다음 다른 인터페이스를 열고 이 인터페이스에 많은 문자가 있을 때 128x128 FontTexture가 충분하지 않으면 Unity는 새 FontTexture를 생성하고 콜백을 발생시킵니다.

(3) 그런 다음 NGUI의 UILabel에서 이 콜백을 처리하고 모든 UILabel을 트래버스하고 UILabel의 모든 내용을 Font에 푸시하여 FontTexture에 대한 새 텍스트 그래픽을 생성합니다.

static BetterList<UILabel> mList = new BetterList<UILabel>();
protected override void OnInit ()
{
    base.OnInit();
    mList.Add(this);
    SetActiveFont(trueTypeFont);
}

 

//每次新建一个UILabel,都会读取UILabel上面的内容,在FontTexture上添加一块字体纹理。当FontTexture超过了原来的大小时,就会抛弃原来的FontTexture,用一个新的FontTexture,然后遍历所有的UILabel,往新的FontTexture上添加字体纹理。
static void OnFontTextureChanged ()
{
    for (int i = 0; i < mList.size; ++i)
    {
        UILabel lbl = mList[i];
 
        if (lbl != null)
        {
            Font fnt = lbl.trueTypeFont;
 
            if (fnt != null)
            {
                fnt.RequestCharactersInTexture(lbl.mText, lbl.mPrintedSize, lbl.mFontStyle);
            }
        }
    }
 
    //这段代码让UILabel重新读取UV 重新渲染,下一帧文字就是正常的了
    for (int i = 0; i < mList.size; ++i)
    {
        UILabel lbl = mList[i];
 
        if (lbl != null)
        {
            Font fnt = lbl.trueTypeFont;
 
            if (fnt != null)
            {
                lbl.RemoveFromPanel();
                lbl.CreatePanel();
            }
        }
    }
}


(4) 마지막으로 UILabel이 다시 렌더링됩니다.

깨진 글꼴은 (2)에서 나타납니다 UILabel이 이미 렌더링 중일 때 Unity는 새 FontTexture를 생성하므로 현재 프레임에 표시되는 그래픽에 문제가 있을 것입니다.

해결책은 게임의 모든 캐릭터를 포함하여 처음에 글꼴에 충분한 텍스트를 제공하여 향후 게임을 푸시할 필요가 없고 FontTexture를 영원히 사용할 수 있도록 하는 것입니다.

UILabel에 기능 추가

    private static void DynamicFontBrokenFix(Font fnt)
    {
        if(fnt==null)
        {
            return;
        }
 
        if(fnt.characterInfo.Length<500)
        {
            string tmpStr = Resources.Load<TextAsset>("DynamicFontBrokenFix").text;
            fnt.RequestCharactersInTexture(tmpStr, 32);
        }
    }


그런 다음 글꼴을 설정할 때 호출

protected void SetActiveFont (Font fnt)
{
    if (mActiveTTF != fnt)
    {
        if (mActiveTTF != null)
        {
            int usage;
 
            if (mFontUsage.TryGetValue(mActiveTTF, out usage))
            {
                usage = Mathf.Max(0, --usage);
 
                if (usage == 0)
                {
                    mActiveTTF.textureRebuildCallback = null;
                    mFontUsage.Remove(mActiveTTF);
                }
                else
                {
                    mFontUsage[mActiveTTF] = usage;
                }
            }
			else
            {
            	mActiveTTF.textureRebuildCallback = null;
            }
		}
 
		mActiveTTF = fnt;
        DynamicFontBrokenFix(mActiveTTF);
 
		if (mActiveTTF != null)
		{
			int usage = 0;
 
			// Font hasn't been used yet? Register a change delegate callback
			if (!mFontUsage.TryGetValue(mActiveTTF, out usage))
				mActiveTTF.textureRebuildCallback = OnFontTextureChanged;
#if UNITY_FLASH
			mFontUsage[mActiveTTF] = usage + 1;
#else
			mFontUsage[mActiveTTF] = ++usage;
#endif
		}
	}
}


DynamicFontBrokenFix는 게임의 모든 문자를 저장하는 텍스트입니다.
http://blog.csdn.net/huutu http://www.liveslives.com에서 전송됨

FontTexture가 여러 번 생성되는 것을 방지하기 위한 최적화 지점이기도 합니다.

 

[출처] https://blog.csdn.net/huutu/article/details/61923191

 

NGUI UILabel 文字破碎__Captain的博客-CSDN博客

项目使用NGUI,最近碰到 Loading界面的提示文字破碎的Bug。 参考了以下文章 http://blog.csdn.net/langresser_king/article/details/22095235 转自http://blog.csdn.net/huutu http://www.liveslives.com 下面先来了解一下为什么会

blog.csdn.net

 

반응형
Posted by blueasa
, |