Author Topic: Dynamic font support for NGUI  (Read 114368 times)

zh4ox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Dynamic font support for NGUI
« Reply #15 on: January 08, 2013, 09:49:05 PM »
Unisip, u are awesome, I stiil have much work to do

wsycarlos

  • Guest
Re: Dynamic font support for NGUI
« Reply #16 on: January 08, 2013, 09:55:01 PM »
Hi Unisip,
I have a question, if your font is used in multiple situation, such as GUIText, 3D Text and NGUI's UIFont, how could that be possible to get every string that used with that font, is there any API about this? Otherwise, if you create a GUIText, and enter something, and the texture is like 256*256, and full of chars, and then now u create a 3D Text contains different chars, so the font texture will rebuilt as you request many new characters. And now, the font didn't know the old GUIText right? So it rebuilt the texture without old characters? And the old GUIText get the callback, and request his chars again, makes the font texture rebuilt again... how to solve this? Or any things I mentioned is wrong? Am I considered too much?

wuming0108

  • Guest
Re: Dynamic font support for NGUI
« Reply #17 on: January 08, 2013, 11:13:07 PM »
awesome!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!great work!!!!!!!!!!!!!!!!!

realblues

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Dynamic font support for NGUI
« Reply #18 on: January 08, 2013, 11:48:04 PM »
reallllly Great!!

Unisip

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Dynamic font support for NGUI
« Reply #19 on: January 09, 2013, 03:38:12 AM »
The Unity APIs do all the work for you.
Basically, there is a delegate that alerts you when the font texture has to be rebuilt from scratch (that is, when there is no more room for a new character). All you need to do is make sure that all your assets using the font get notified so they can take action.
In the case of UILabels, when a font texture is rebuilt, call 'MarkAsChanged' on every UILabel that uses the font does the job.

Again, for anyone programmer who wants to play around with that code, I'm happy to share it "as is".

zh4ox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Dynamic font support for NGUI
« Reply #20 on: January 09, 2013, 06:15:41 AM »
the work slows down because of multiple font sizes in one font texture --- it's very hard to place the text correctly,  but finally it solved, though I don't think myself's solution is perfect :( (u must first set the TTF's font size to ONE), btw, anyone have a better way to solve the problem?
Here is the pic, all text with different sizes (16, 24, 30, 36, 100) are got from the same one dynamic font texture, and their offsetX, offsetY is approximatively correct, all inside the box.


« Last Edit: January 09, 2013, 06:19:53 AM by zh4ox »

sk8er123

  • Guest
Re: Dynamic font support for NGUI
« Reply #21 on: January 09, 2013, 10:42:17 AM »
how download source code ???????????

Unisip

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Dynamic font support for NGUI
« Reply #22 on: January 09, 2013, 12:11:35 PM »
@zh4ox
Mmm, I'm not sure I am following you on this one. Why would you have to change any settings in the font asset, like the size? In my implementation, I never had to worry about this, neither did I have to bother much about text placement, it's all handled by the CharacterInfo class in Unity.

jarjin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Dynamic font support for NGUI
« Reply #23 on: January 09, 2013, 08:52:49 PM »
all size font in single texture?

zh4ox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Dynamic font support for NGUI
« Reply #24 on: January 09, 2013, 10:02:09 PM »
@sk8er123
Please take patience, when the work is done, I'll upload the code.
@jarjin
Yes, all size font in single texture, because I want the draw call as few as possible
@Unisip
I've ever try not change the size setting in the font asset, but failed. May I know how u calculate the v0.y to make all text inside the box?  e.g. the font asset's name is YouYuan, with size 16, the characterInfo.vert.y of 国 with size 16 is correct, it's -0.125f, so just set v0.y=(y - 0.125f)*scale.y is OK, but the characterInfo.vert.y of 国 with size 64 is 35.88f, and then what's the correct v0.y?
« Last Edit: January 09, 2013, 10:07:19 PM by zh4ox »

wsycarlos

  • Guest
Re: Dynamic font support for NGUI
« Reply #25 on: January 09, 2013, 10:33:10 PM »
Regarding memory management, the beauty of it is that the texture tends to use only the characters that are in use at a given moment. That is, suppose you have a font texture that holds a lot of 72px characters because you are showing a splashscreen GUI, for instance, next when you move on to another screen, like for instance an inventory menu, if you start using smaller characters of the same font, say 16px for titles and 12px for subtitles, whenever the texture is full it will be rebuilt, but without the 72px characters since they are nolonger in use on screen.

I've got my own integration mostly working and I am happy to share it too for free. At first I considered putting a price on it, but I'm happy to share it freely as every one seems excited about this. As long as I don't have to answer zillions of questions about it, I'm happy to share.

Eventually, ArenMook, I really think you should get down to integrating this in NGUI. It's not a lot of code, and what worries me about my own solution or any solution I could pick up on this forum is maintainance --> since I basically modified some of your source code, everytime you submit an update in assetstore, it's still going to be a bit clumsy to apply my patch on top of it.

BTW, Unisip, thank you for your answer,but you didn't get my point.
You mentioned about "whenever the texture is full it will be rebuilt, but without the 72px characters since they are no longer in use on screen", but zh4ox have tested that, the texture will rebuild when it is full, but Unity won't remove unused characters from the texture, and I didn't find any API about this. What did you do to remove old characters?

jarjin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Dynamic font support for NGUI
« Reply #26 on: January 09, 2013, 11:25:00 PM »
@zh4ox @wsycarlos Thank you for your work. If you can remove unused characters from the texture, The perfect solution!!~~~

Unisip

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Dynamic font support for NGUI
« Reply #27 on: January 10, 2013, 03:04:31 AM »
Hi all,

With permission from ArenMook, I'm releasing this code that adds support for Unity 4 dynamic fonts in NGUI.
This code is provided "as is", it hasn't been fully tested on all platforms, and there might be bugs. Feel free to modify it as needed, and to integrate it in your own products, as long as you have an NGUI source licence.

Please look at the readme file for help.

@wsicarlos: what I found in my tests was the following:
- when you add new chars to the font texture, if the texture has to be rebuilt (to change its size), it will only be rebuilt with those new chars. All older chars are removed, and the texturerebuilt callbacks are invoked. On callback, you can have other gameobjects displaying text (UILabels, but also text meshes if needed) request their characters to be added to the font texture again. Any object that doesn't request its characters again takes the risk of not finding them in the texture anymore. Anyway, see my code for it, a diff with the original NGUI version will point you to the changes I made.

@zh4ox: you can see in my code how height is handled. The multiline text is displayed properly. The only thing that is not working well in my code yet is the pivot vertical alignment. I'm not sure where the problem is, but I'll try to figure it out too when I have more time (busy finishing another project right now).

Any feedback appreciated...

zh4ox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Dynamic font support for NGUI
« Reply #28 on: January 10, 2013, 03:48:04 AM »
Quote
@Unisip
I've ever try not change the size setting in the font asset, but failed. May I know how u calculate the v0.y to make all text inside the box?  e.g. the font asset's name is YouYuan, with size 16, the characterInfo.vert.y of 国 with size 16 is correct, it's -0.125f, so just set v0.y=(y - 0.125f)*scale.y is OK, but the characterInfo.vert.y of 国 with size 64 is 35.88f, and then what's the correct v0.y?
Nice job, man. I've read your code and test it. But the problem what I have mentioned seems not solved. Here is ur program's result -- all the text is above the top corner.



jarjin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: Dynamic font support for NGUI
« Reply #29 on: January 10, 2013, 03:58:20 AM »
Thank you ever so much!!!!!!!!!!!!!!!!!!!!!!