2D Toolkit Forum

2D Toolkit => Support => Topic started by: steviebob on August 23, 2014, 09:27:06 am

Title: How To Programmatically Modify A Layout Buttons Text?
Post by: steviebob on August 23, 2014, 09:27:06 am
Hello,
I'm trying to create a button where I can change it's text programmatically, but I can't seem to figure it out.
Before I started using the 2dToolkit, I would do this in the OnGUI method and simply create the button a new but with different text.

I think the toggle button would probably do what I want, but I still want to learn how to change to text to any arbitrary string, rather than just 1 of 2 possibilities.

While I'm on the topic of buttons, the way I'm handling their various events is to attach a script to the camera, and make each button a public property within the script.  Is this a recommended approach?

Any help would be appreciated.
Oh, also, I'm really enjoying the 2dToolkit experience so far. Previously I used to waste a ridiculous amount of time just trying to position things correctly, now it looks like it's all done for me, a massive time saver.  I think I still need to learn camera overrides to get the most benefit out of the tool, but it's great so far.

Thanks.
Title: Re: How To Programmatically Modify A Layout Buttons Text?
Post by: unikronsoftware on August 23, 2014, 11:08:30 am
Quote
While I'm on the topic of buttons, the way I'm handling their various events is to attach a script to the camera
We use various different things depending on the scenario. Eg, if you create dialog box you can simply attach a behaviour to it to control it.

To change text - the button doesn't know about the text meshes or whatever that form the button itself. You just need to get to the textmesh object somehow. Eg. button.transform.Find("ChildObject/AnotherChild/TextMesh").GetComponent<tk2dTextMesh>().text = "Hello";

Or you could store references to the text mesh in your code.
Title: Re: How To Programmatically Modify A Layout Buttons Text?
Post by: steviebob on August 23, 2014, 07:12:39 pm
That worked perfectly, thanks a lot.