这是按钮的好方法吗?
我正在使用Unity的按钮进行练习,并且正在尝试弄清楚如何在不使用检查员中使用OnClick的东西的情况下将不同的方法分配给按钮,因此我想到了。
public class UIButtons : MonoBehaviour
{
void Start()
{
var buttons = FindObjectsOfType<Button>(); //I should probably have this outside of this method
foreach (Button button in buttons)
{
button.onClick.AddListener(() => ButtonPressed(button));
}
}
void ButtonPressed(Button button)
{
switch (button.name)
{
case "MMPlayButton": //The main menu play button
Debug.Log("Play button pressed");
break;
case "PlayButton":
Debug.Log("Play button pressed");
break;
case "SettingsButton":
Debug.Log("Settings button pressed");
break;
case "QuitButton":
Debug.Log("Quit button pressed");
break;
default:
Debug.LogWarning("Button doesn't have a case: " + button.name);
//could do some kind of pop up message saying the button pressed isn't available or something
break;
}
}
}
我知道这可以起作用,但是,我想这是一种可怕的方法,因为它正在使用该名称,因此,如果我要更改名称或制作错字,或者我遇到的问题是,如果按钮有按钮同样的名字,如果我添加更多按钮,我必须添加更多案例,然后可能会变成一个巨大的混乱。
不过,我可能是错的,也许这是一个很好的主意,但是我怀疑这对此有所帮助。
I'm practicing with buttons in Unity and I'm trying to figure out how I can assign different methods to a button without using the OnClick thing in the inspector so I came up with this.
public class UIButtons : MonoBehaviour
{
void Start()
{
var buttons = FindObjectsOfType<Button>(); //I should probably have this outside of this method
foreach (Button button in buttons)
{
button.onClick.AddListener(() => ButtonPressed(button));
}
}
void ButtonPressed(Button button)
{
switch (button.name)
{
case "MMPlayButton": //The main menu play button
Debug.Log("Play button pressed");
break;
case "PlayButton":
Debug.Log("Play button pressed");
break;
case "SettingsButton":
Debug.Log("Settings button pressed");
break;
case "QuitButton":
Debug.Log("Quit button pressed");
break;
default:
Debug.LogWarning("Button doesn't have a case: " + button.name);
//could do some kind of pop up message saying the button pressed isn't available or something
break;
}
}
}
I know this can work, however, I'd imagine this is a horrible way to do things because it's using the name so if I were to change the name or make a typo it breaks, or an issue I did encounter was if buttons have the same name, and if I add more buttons I'd have to add more cases and then it would probably turn out to be a giant mess.
I could be wrong though and maybe this is an alright idea, but I doubt it so looking for some help with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用继承来简化这一点。为您的所有按钮创建一个基类,即。
uibuttonbase
具有OnButtonPressed
虚拟方法。然后创建继承此基类的特定按钮,并覆盖onttonpressed
方法。基类
特定的按钮
进行设置,创建一个按钮并将此脚本添加到其中。您可能需要将
getComponent
在中
(uibuttonbase)中更改为getComponentInparent
或getComponentInChildren
,取决于您的GameObjects hierSarchys hierSarchys hierSharchy 。您还可以在UibuttonBase脚本中曝光按钮组件,并使用该参考。You can simplify this by using inheritance. Create a base class for all of your buttons to use, ie.
UIButtonBase
that has theOnButtonPressed
virtual method. Then create specific buttons that inherit this base class and override theOnButtonPressed
method.Base Class
Specific implementation of a button
For setup, create a button and add this script to it. You may need to change the
GetComponent
inAwake
(UIButtonBase) toGetComponentInParent
orGetComponentInChildren
, depending on your gameobjects hierarchy. You could also expose the Button component in the UIButtonBase script and use that reference.我知道这不是一个好答案,但是如果我是您,我会为不同的按钮制作不同的文件。像setterButton.cs,playbutton.cs等。
这种方法不会为您的代码增加负担,但是随着按钮的功能增加,这种方法将极大地帮助您井井有条。
I know this won't be a good answer but if I were you, I would make different files for different buttons. Like SettingButton.cs, PlayButton.cs etc.
This approach will not add a huge amount of burden to your code but as the functionalities of your buttons increase, this approach will greatly help to keep your code organized.