Author Topic: NGUI and coding: best practice for communication between obejcts?  (Read 18489 times)

andrejvojtas

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 19
    • View Profile
How do I hook a variable (e.g. an int) to a Label created in Editor? To display score and such.

How do I create an UITexture on runtime?

Thank you.

EDIT: I changed the title to better reflect what I meant.
« Last Edit: May 01, 2013, 01:47:30 PM by andrejvojtas »

dlewis

  • Guest
Re: NGUI and coding: how do I display a variable in a Label?
« Reply #1 on: April 30, 2013, 09:02:04 PM »
Displaying a variable: Format a string and apply it.
myLabel.text = "Score: " + myVariable;
myLabel.text = string.format("Score: {0}", myVariable);

Create UITexture
GameObject go = new GameObject();
go.AddComponent<UITexture>();
NGUITools.AddChild(Something).

andrejvojtas

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 19
    • View Profile
NGUI and coding: best practice for communication between obejcts?
« Reply #2 on: May 01, 2013, 01:45:22 PM »
Thanks, what I was after is something else, I didn't explain it very well.

What is the best practice for communicating between the UIwidgets. e.g. I have 20 UI buttons clicks that change rpg stats in a central game object (lets call it "hero") and then I need the stats from the "hero" object to display in the 20 UI Labels.

What do I do for the buttons? Add scripts that will on click go use GameObject.Find, then set the script component, then adjust the variables?
Again what do I do for the labels? Do they also need to go find the "hero" object, the correct component and read the variables?

Or should I use listeners and messaging, as in e.g.: the notification center: http://wiki.unity3d.com/index.php?title=NotificationCenter

What is efficient performance wise? Is there a best practice for NGUI / Unity to do this?

sonicviz

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 58
    • View Profile
Re: NGUI and coding: best practice for communication between obejcts?
« Reply #3 on: May 01, 2013, 09:51:24 PM »
You can use http://wiki.unity3d.com/index.php/Advanced_CSharp_Messenger (which is what I do)
NGui also has an inbuilt messaging system.

There's also http://forum.unity3d.com/threads/127918-NData-MVVM-framework-for-NGUI if you want to implement an MVVM approach, which you can combine with Messaging above.

andrejvojtas

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 19
    • View Profile
Re: NGUI and coding: best practice for communication between obejcts?
« Reply #4 on: May 01, 2013, 11:06:12 PM »
Thank you, I will check those out.

What is the benefit of using the advanced C# messenger over the one inbuilt in NGUI?

sonicviz

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 58
    • View Profile
Re: NGUI and coding: best practice for communication between obejcts?
« Reply #5 on: May 01, 2013, 11:17:03 PM »
NGui one can be also used as a general event messenger system afaik, but I have not used it so can't really say the pros/cons.
I used the C# Messenger on a previous project, so just continued with it on current one as I was used to it.
I remember looking at the NGui one but decided to stick with messenger, I think it may have been a little more flexible for my needs at the time.

You should probably analyze them both yourself to get a better understanding of the basic messaging approach and see which one fits best.
You would need to do this for whichever one you choose anyway, and doing the extra comparison should be easy once you have the concepts down.

andrejvojtas

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 19
    • View Profile
Re: NGUI and coding: best practice for communication between obejcts?
« Reply #6 on: May 03, 2013, 07:49:20 AM »
Thank you for you reply, it's been very useful.