Author Topic: How to change dynamically the target of a NGUI UIButtonMessage?  (Read 2959 times)

newlife

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
How to change dynamically the target of a NGUI UIButtonMessage?
« on: January 27, 2014, 04:07:17 AM »
Hello,
I have a complex interface made with NGUI. I have several UIBuottomMessage (plus one slider button) with target set via inspector.
Now i need to change the target of these buttons runtime; is there a quick and elegant way to implement this? (apart of writing a specific code for all those buttons (they may change in the future).
thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: How to change dynamically the target of a NGUI UIButtonMessage?
« Reply #1 on: January 27, 2014, 12:15:53 PM »
UIButtonMessage is one of those scripts that was added as a "here's how it can be done" things. I would recommend you use UIEventListener instead when going through code. Event listener lets you subscribe to remote events in a much more efficient manner than UIButtonMessage can be used to forward them.

newlife

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: How to change dynamically the target of a NGUI UIButtonMessage?
« Reply #2 on: January 28, 2014, 04:27:06 AM »
UIButtonMessage is one of those scripts that was added as a "here's how it can be done" things. I would recommend you use UIEventListener instead when going through code. Event listener lets you subscribe to remote events in a much more efficient manner than UIButtonMessage can be used to forward them.

thank you for your reply. In this way i have to create a UI manager and replicate all the UI in it (that is add a parameter for each button and slider that im using). Is there a way to simply change the target gameobject for example by replacing it with another gameobject?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: How to change dynamically the target of a NGUI UIButtonMessage?
« Reply #3 on: January 28, 2014, 09:59:23 AM »
GetComponent<UIButtonMessage>().target = newTarget;

newlife

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: How to change dynamically the target of a NGUI UIButtonMessage?
« Reply #4 on: January 28, 2014, 04:00:12 PM »
GetComponent<UIButtonMessage>().target = newTarget;

well this is what i would like to avoid, since my UI is complex and open to future changes. In this way i will need to change the code every time the UI changes.
What i need is a way to change in runtime the gameobject itself that is bounded to the various buttons and sliders, not the target of every buttons.
Is there a way to accomplish this with Unity or with NGUI?
thanks.

newlife

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: How to change dynamically the target of a NGUI UIButtonMessage?
« Reply #5 on: January 29, 2014, 04:11:19 AM »
The only solution i found is to attach the new gameobject created in runtime as a child of the old target gameobject and set all buttons IncludeChildren to true. More, i add to modify the code of UISlider in order to add IncludeChildren parameter.

Isnt really not possible with Unity to copy a gameobject into another via code??

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: How to change dynamically the target of a NGUI UIButtonMessage?
« Reply #6 on: January 29, 2014, 09:19:17 AM »
I don't quite understand what you're trying to do... If you need something changed like that, why not have your button notify a manager script, and have the manager do the rest? Why do you need to change the target? Central manager script is the key.

newlife

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: How to change dynamically the target of a NGUI UIButtonMessage?
« Reply #7 on: January 30, 2014, 01:42:25 PM »
I don't quite understand what you're trying to do... If you need something changed like that, why not have your button notify a manager script, and have the manager do the rest? Why do you need to change the target? Central manager script is the key.

Cause in this way i have to manage all the uibuttons and sliders in this manager (there are several buttons with two different uibuttonsendmessage scripts, one used to close the window and the other to send the message to the manager plus the uislider script). So in this way i have to assign the central manager all uibuttonsendmessage and uislider script to the manager, and every time the UI changes, i have to change the central manager code accordingly.

While, if i change directly the target game object object, i dont have to change anything in the code, just the UI in the inspector.
Hope now its more clear.