Hello Guest

Author Topic: tk2dTextMesh set font with script  (Read 7126 times)

Fohlin

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
tk2dTextMesh set font with script
« on: July 25, 2013, 12:47:32 pm »
I'm creating a new gameobject and attaching a tk2dTextMesh component to it, and I want to also set the font it should be using. I see that there is a font variabel I could set, but I couldn't figure out how to use it properly. I have created a custom font and want to use that. Can I get my font through the tk2dFontData in some way? I know there is a way to create a prefab and use that one, but I rather not : ) Thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dTextMesh set font with script
« Reply #1 on: July 25, 2013, 01:00:58 pm »
You'd have to access to the tk2dFontData somehow. You can do this using an explicit link (drag & drop) in your invoking script, or using Resources.Load. Once you get that you can simply assign it.

Fohlin

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: tk2dTextMesh set font with script
« Reply #2 on: July 25, 2013, 01:11:51 pm »
Yes I just solved it!
I put the custom FontData file that was created (automatically?) in Assets/Fonts/Resources so I could retrieve it, like this:
GameObject fontData = Resources.Load("TrollFontData") as GameObject;
textmesh.font = fontData.GetComponent<tk2dFontData>();

Thank you for the fast reply!

Fohlin

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: tk2dTextMesh set font with script
« Reply #3 on: October 16, 2013, 10:29:35 am »
Hi again. I have a kind of related problem now with this textmesh and using characters like Å Ä Ö in it. When I type the characters into the textmesh from the Inspector in Unity they show up correctly in the textmesh, but not when using script: textmesh.text = "åäö"; textmesh.Commit();
How can I send this string to the textmesh the right way?. Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dTextMesh set font with script
« Reply #4 on: October 16, 2013, 10:44:18 am »
That is the right way, but your .cs script likely isn't saved in the correct format. Save it as utf8 and try again. Otherwise, use unicode escape codes (\u0000) to get the characters under any encoding.

Fohlin

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: tk2dTextMesh set font with script
« Reply #5 on: October 16, 2013, 10:57:54 am »
Thank you, it was the file format that was strange. I couldn't figure out how to change the encoding from within Monodevelop-Unity, and saw lots of people recommending Notepad++(win) or TextWrangler(mac) for this. So when I saved the .cs file as utf-16 from TextWrangler it worked. A bit strange that it didn't work in utf-8 format.

But it's working now atleast :)

EDIT: Saved as UTF-8, with BOM from Textwrangler also worked
« Last Edit: October 16, 2013, 11:01:29 am by Fohlin »