这是按钮的好方法吗?

发布于 2025-01-24 13:21:47 字数 1373 浏览 1 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯狂的代价 2025-01-31 13:21:47

您可以使用继承来简化这一点。为您的所有按钮创建一个基类,即。 uibuttonbase具有OnButtonPressed虚拟方法。然后创建继承此基类的特定按钮,并覆盖onttonpressed方法。

基类

public class UIButtonBase: MonoBehaviour
{
    protected virtual void Awake()
    {
        var button = GetComponent<Button>();
        if (button)
        {
            button.onClick.AddListener(() => OnButtonPressed());
        }
        else
        {
            Debug.Log(name + " does not have a Button component!");
        }
    }
    
    protected virtual void OnButtonPressed()
    {
        Debug.Log("UIButtonBase::OnButtonPressed()");
    }
}

特定的按钮

public class SettingsButton : UIButtonBase
{
    protected override void OnButtonPressed()
    {
        Debug.Log("SettingsButton::OnButtonPressed()");

        // If you want the base functionality as well, add..
        //
        base.OnButtonPressed();
    }
}

进行设置,创建一个按钮并将此脚本添加到其中。您可能需要将getComponent(uibuttonbase)中更改为getComponentInparentgetComponentInChildren,取决于您的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 the OnButtonPressed virtual method. Then create specific buttons that inherit this base class and override the OnButtonPressed method.

Base Class

public class UIButtonBase: MonoBehaviour
{
    protected virtual void Awake()
    {
        var button = GetComponent<Button>();
        if (button)
        {
            button.onClick.AddListener(() => OnButtonPressed());
        }
        else
        {
            Debug.Log(name + " does not have a Button component!");
        }
    }
    
    protected virtual void OnButtonPressed()
    {
        Debug.Log("UIButtonBase::OnButtonPressed()");
    }
}

Specific implementation of a button

public class SettingsButton : UIButtonBase
{
    protected override void OnButtonPressed()
    {
        Debug.Log("SettingsButton::OnButtonPressed()");

        // If you want the base functionality as well, add..
        //
        base.OnButtonPressed();
    }
}

For setup, create a button and add this script to it. You may need to change the GetComponent in Awake (UIButtonBase) to GetComponentInParent or GetComponentInChildren, depending on your gameobjects hierarchy. You could also expose the Button component in the UIButtonBase script and use that reference.

蘸点软妹酱 2025-01-31 13:21:47

我知道这不是一个好答案,但是如果我是您,我会为不同的按钮制作不同的文件。像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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文