Hello Guest

Author Topic: Changing the prerendered text  (Read 6087 times)

ENAY

  • Newbie
  • *
  • Posts: 17
    • View Profile
Changing the prerendered text
« on: April 09, 2012, 07:59:16 am »
Ok, just getting started with this library, Ok with Unity so far but am having a few simple problems getting started. Should be ok once I get going. I have a text mesh saying "Hello", I named it as "Text" and placed it in the Hierarchy windows. What I simply want to do is change the text in code.

Basically I want to do something like this:-  (C#)

---------------------------------------------
myText = GameObject.Find("Text");
      
myText.text("moose");
-----------------------------------------------------

However this doesn't work. I have seen a _text variable in the tk2dTextMesh.cs, all I want to do is change the text. Maybe I am not fully understanding how these meshes are made and maybe they can't be changed at runtime.

Ideally I want to simply instantiate a prefab from a string text and change it later. Or something similiar to this:-

meshTest = new tk2dTextMesh();
meshTest = tk2dTextMesh.text("moose");

I am trying to make "Score = 000000" with changing numbers.

Hopefully someone can point me in the right direction to help me get started.
I hope my question makes sense, thanks for reading.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing the prerendered text
« Reply #1 on: April 09, 2012, 09:21:40 am »
To do it in C# -
http://www.unikronsoftware.com/2dtoolkit/doc/tutorial/Scripting_a_text_mesh.html

This script needs to be attached to the textmesh itself. If you want to host the script elsewhere, simply make the textMesh parameter public, and drag a link to attach it. Generally, try to avoid GameObject.Find, etc. as you can explicitly manage relationships and performance is much better.

ENAY

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Changing the prerendered text
« Reply #2 on: April 09, 2012, 10:13:43 am »
Hello Unikron,

Thanks for the prompt reply and the link. I think possibly GetComponent is maybe what I was looking for and not GameObject.Find. I know that Find is slow, I don't plan on using it as later, I am hoping to be able to create the Text meshes in code so I don't have to find them. Which means I can't really drop and drop a script onto it as it won't exist at runtime.

I have sprites and animsprites working though. I created a collection of all my gfx, then made a sprite, copied it into the Project Menu and now I make clones of it. So far I have about 128, as soon as the game compiles it creates them all in one go and then moves them off screen, now I have a list which allows me to determine which sprites are in use and whether they should be moved on screen.

I am presuming this is the fastest way to use your library?

Just hoping to be able to fix the tesh meshes thing by changing the text and then I think I am on my way.

I will let you know later how I got on, or if I have any more issues. Thanks for your time :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing the prerendered text
« Reply #3 on: April 09, 2012, 12:22:10 pm »
You don't have to move them off screen, you can simply set GameObject.active = false on the object. At least that way the object won't have to be considered for culling at all. Pooling the data like you are is definitely the way to go.

With text meshes, all you need to do is create one, make a prefab out of it and Instantiate the prefab as many times as you like. You could do that with the sprites too if you aren't already doing it.

ENAY

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Changing the prerendered text
« Reply #4 on: April 09, 2012, 01:08:57 pm »
Thanks for the tip on GameObject.active = false. I will still move them off screen since it makes it easier to debug all your stuff sifting through numbers if inactive sprites have fairly large negative numbers.

I am already Instantiating out of various prefabs for everything, I managed to finally change the contents of text.

I have all my stuff working now. Really, big thanks for creating such an excellent library and thanks personally for all your help. :)