Unity 5:如何在检查器的按钮单击功能上传递多个参数? [英] Unity 5: How to pass multiple parameters on button click function from inspector?
问题描述
我想在检查器点击按钮时发送多个参数?
I want to send multiple parameter on button click from inspector?
Unity 不允许这样做,我该如何实现?还有其他方法吗?有几十个按钮,我对所有按钮使用相同的方法,但发送不同的参数,现在我需要发送多种类型的参数.想不通怎么办?
Unity isn't allowing that to do, how can I achieve that? is there any other way? There are dozens of buttons and I am using same method for all buttons but sending different parameters, now I need to send multiple types of parameters. Can't figure out how?
推荐答案
从编辑器调用 Unity 函数有限制.只能带一个参数,函数必须是非static
函数.
Calling Unity function from the Editor has limitation. It can only take one parameter and the function must be a non static
function.
这就是为什么知道如何从代码订阅事件很重要的原因.下面是一个在单击 Button
时调用的函数示例.它将一个字符串和一个整数传递给该函数.很简单,将 Button
拖到编辑器中的 some Button 插槽,这应该可以工作.
This is why it is important to know how to subscribe to events from code. Below is an example of a function that gets called when a Button
is clicked. It passes in a string and an integer to that function. Simple, drag the Button
to the some Button slot in the Editor and this should work.
public Button someButton;
void OnEnable()
{
//Register Button Events
someButton.onClick.AddListener(() => buttonCallBack("Hello Affan", 88));
}
private void buttonCallBack(string myStringValue, int myIntValue)
{
Debug.Log("Button Clicked. Received string: " + myStringValue + " with int: " + myIntValue);
}
void OnDisable()
{
//Un-Register Button Events
someButton.onClick.RemoveAllListeners();
}
这篇关于Unity 5:如何在检查器的按钮单击功能上传递多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!