2D Toolkit Forum

2D Toolkit => Support => Topic started by: spartan on May 21, 2012, 01:52:11 am

Title: tk2dButton
Post by: spartan on May 21, 2012, 01:52:11 am
Hi @unikron, I was wondering if exists a way to get which gameObject sent the message of a pressed button (tk2dButton). I have implemented a very simple trick that maybe you can include on your next update.
Replace Line 295 from tk2dButton by:
targetObject.SendMessage(messageName, gameObject.name);

And the function of the receive can get on the first parameter the message sender, this is very useful if you have few or a lot of buttons pointing to a general function and you need to identify which button has been pressed (eg: which level).
Title: Re: tk2dButton
Post by: unikronsoftware on May 21, 2012, 02:56:52 am
I'll add this, but I think it should send the gameObject as a reference instead of the name. You can get the name from the GameObject, and also any additional components, which will be really useful.

Got a question for you - What happens if the SendMessage target doesn't have a signature accepting a string in your case?
Title: Re: tk2dButton
Post by: MetalGreg on May 26, 2012, 03:11:47 pm
Maybe you could make that the function we call have a GameObject parameter ?
It's not a top 1 priority but it would save some lines for sure.
Quote
public void ButtonEvent(GameObject YouClickedThisOne){}
Title: Re: tk2dButton
Post by: unikronsoftware on May 27, 2012, 02:53:33 pm
Do you mean for the delegate or the event callback? I took a quick look the other day, but I think it should be a tk2dButton component, at least for consistencies sake, and in the interest of backwards compatibility. In hindsight, I do agree that the source should have been a GameObject.
Title: Re: tk2dButton
Post by: fsadeq on June 13, 2012, 02:28:08 pm
I think there should be an option for parameters to the SendMessage call on the button. Right now I am modifying it myself, but I'd like to keep it up to date with the latest release version.
Title: Re: tk2dButton
Post by: unikronsoftware on June 14, 2012, 04:52:41 pm
I checked - if SendMessage with a target doesn't have a target, Unity does throw up an error, so I can't simply replace the behavior directly. I'm investigating a much nicer approach for my next gen UI components, but that so far doesn't work with the flash player export.
Title: Re: tk2dButton
Post by: tjgillis on September 26, 2012, 04:15:12 pm
Hey all,
I had the same issue as the OP but I also didn't want to break the default behaviour and have what I think is a pretty clean solution if anyone is still looking:

Code: [Select]
public bool sendWithReference = false;

....
if (targetObject)
{
if(sendWithReference){
targetObject.SendMessage(messageName,this.gameObject);
}else{
targetObject.SendMessage(messageName);
}
}
....