- Home /
New UI button OnClick() how to "send message" with parameters?
I have try different ways to send message with/without brackets, commas and different combinations to seperated text with parameters but couldn't get it to work. It'd be great if there is an example but I wasn't able to find it in the refereces.
Answer by nightowl79a · Jan 26, 2015 at 11:32 AM
You might have to zoom in with ctrl + mouse scroll wheel to see the picture, but its just a string with nothing special about it. it calls this script:
public void Testing123(string buttonName)
{
Debug.Log (buttonName);
}
and that makes it work just fine.
Also the function must be public and void, and have one or no parameters. For the parameters you can use float, int, string, bool, or a Unity Object.
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button This is a very nice video to help understand almost everything about the new UI buttons.
The image in your answer is giving me a 403. You could just upload the image as a part of your post ins$$anonymous$$d of using dropbox
Yea sry about that, sometimes my pictures are too big to upload even though I just use the windows snipping tool.
Thanks for the info and please let me explain this a bit more, I'm trying to use the build-in 'Send$$anonymous$$essage' function. There is method name and parameter. The Send$$anonymous$$essage is used differently, I don't want to call a method in the same gameobject/class. So I want to use Send$$anonymous$$essage ins$$anonymous$$d of the solution you showed.
What you could do is set the gameobject that needs to receive the message in the editor, or use a gameobject.find, then in the function activated by the button do something like this:
public GameObject whatGameObject;
// Or use GameObject.find()
public void SendA$$anonymous$$essage(string what$$anonymous$$sg)
{
whatGameObject.Send$$anonymous$$essage ("thefunction", what$$anonymous$$sg);
}
The button would activate the SendA$$anonymous$$essage function, and send the 'what$$anonymous$$sg' variable. Then you would send the message in the function. Ofc if you needed multiple parameters you might need to find a different solution.
Answer by NullTerminated · Aug 15, 2016 at 03:08 PM
void InitMusicListContent()
{
JSONNode musicData = _myAudioPlayer.GetMusicData;
//set the size of it before we start to fill it
_musicListRectTransform.sizeDelta = new Vector2 (_musicListRectTransform.rect.x, _heightOfSongPanel * (musicData.Count + 2));
// for each song in the array, create new panel--> Parent under the Content panel --> Get its components --> init them
for (int i = 0; i < musicData.Count; i++)
{
GameObject songPanel =
Instantiate (_songPanelPrefab, this.transform.position, this.transform.rotation, this.transform) as GameObject;
Text[] textComponents = songPanel.GetComponentsInChildren<Text> ();
foreach (Text text in textComponents)
{
if (text.name == "ArtistNameText") {
text.text = musicData [i] ["ArtistName"];
} else if(text.name == "TrackNameText"){
text.text = musicData [i] ["TrackName"];
}
}
Button songPanelButton = songPanel.GetComponentInChildren<Button> ();
// must assign it to a variable before supplying it to listener
string x = musicData[i]["TrackPath"];
songPanelButton.onClick.AddListener(() => {_myAudioPlayer.AddToMusicQueue(x);} );
}
}
So i was working on some music streaming in a game engine and i ran into the same problem. I wanted the user to be able to choose a song to add to there music Queue while they were playing the game. Because of the sheer amount of songs to choose from, the only good way to subscribe a button to a specific method was programatically. Here, you can see that I have a loop that instantiates a prefab, and on this prefab I had a Button. I needed to subscribe this button to a method that took in a string that represented a path to the song. Because each of these are different for each song, I needed the parameter value that is passed in to be diff for each button. Initially, It didnt work because i did this:
songPanelButton.onClick.AddListener(() => {_myAudioPlayer.AddToMusicQueue(musicData[i]["TrackPath"]);} );
I tried to pass in the track path directly. Once I placed the value in a variable, then it worked. Don't make this same mistake. Its hard to fix. Hope this helps. Cheers mate.
Answer by VRAndAR · Jan 22, 2018 at 07:37 AM
Hopefully This will Help You.If you want to pass the parameters through the inspector, Then make them as the public variables in the script in which the function which calls when the button is present.
public class ButtonScript : MonoBehaviour {
public string message;
public int integer;
public float floatingPointNumber;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void ButtonPressed()
{
//Do Whatever You Want
Debug.Log("Message: " + message + "\t" + "Integer: " + integer.ToString() + "\t" + "Float: " + floatingPointNumber);
}
}
I was about to give this as an answer then i found you have already suggested using a class it self to hold and pass the data... this is an easy way to work around for only having 1 parameter or none but passing in many variables at the same time.... I hope more people see this answer
Your answer
Follow this Question
Related Questions
Unity 4.6 new UI System. Button not working after the canvas is created as prefab. HELP!! 1 Answer
UI.Toggle set to isOn initial state on exit 1 Answer
Can I break the UI MASK hierarchy? 2 Answers
How to stop non-rectangular buttons from overlapping? 4 Answers
How can you set navigation type in code? 0 Answers