简体   繁体   English

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 isn't allowing that to do, how can I achieve that? Unity 不允许这样做,我怎样才能做到这一点? 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?想不通怎么办?

Calling Unity function from the Editor has limitation.从编辑器调用 Unity 函数有限制。 It can only take one parameter and the function must be a non static function.它只能带一个参数,并且函数必须是非static函数。

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.下面是一个在单击Button时调用的函数示例。 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.很简单,将Button拖到编辑器中的某个 Button插槽中,这应该可以工作。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM