Is there a way to have multiple integers as function arguments when using the UI Button?
I know you can split strings, but I need integers.
As far as I know you can only have a single function argument to the button.
Is there a way to have multiple integers as function arguments when using the UI Button?
I know you can split strings, but I need integers.
As far as I know you can only have a single function argument to the button.
Make a class that contains multiple ints and use that for the argument.
Or you can actually add as many OnClick functions as you want, they all get called at the same time and just get update to do whatever you want after setting a bool to true e.g.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MyButton : MonoBehaviour {
public Text myText;
private bool IsClicked = false;
private int intOne;
private int intTwo;
private string stringOne;
private string stringTwo;
// Update is called once per frame
void Update () {
if(IsClicked)
{
myText.text = " = " + stringOne + " : " + stringTwo + " : " + intOne.ToString() + " : " +intTwo.ToString();
IsClicked = false;
}
}
public void StringOne(string String1)
{
stringOne = String1;
Debug.Log (stringOne);
IsClicked = true;
}
public void StringTwo(string String2)
{
stringTwo = String2;
}
public void IntOne(int Int1)
{
intOne = Int1;
}
public void IntTwo(int Int2)
{
intTwo = Int2;
}
}
Attach this in your button or make a referece to it on a empty game object on the scene.
Then use the two ints x,y as your args.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TwoArgs : MonoBehaviour
{
//your button
public Button b;
//control the value to pass to event as you need
public int x, y;
void Start()
{
//register new event to onclick with the variables that control your args
b.onClick.AddListener(() => CustomClick(x, y));
}
public void CustomClick(int a, int b)
{
//do something you want
print(a + b);
}
}
a bit late, i know @MrMelonPie, but for anybody else looking for a simple solution …
// call this variable and assign myInteger at OnClick() of the UI button
public int myInteger { private get; set; }
// call this function and assign myFloat at OnClick() of the UI button
public void ButtonClickFunction(float myFloat)
{
DoSomethingWithMyParameters(myInteger, myFloat);
}
should do it.