【问题标题】:Refactor the button function?重构按钮功能?
【发布时间】:2022-11-22 19:20:16
【问题描述】:

我有很多这个按钮功能,唯一不同的是 GameObject[] 通过单例。

像这样

public void Oneselect()
{
   GameObject[] typeor = Manager.singleton.arrayOne; 
  dosomething();
}
public void Twoselect()
{
   GameObject[] typeor = Manager.singleton.arrayTwo;
  dosomething();
}
.
.
.
public void Tenselect()
{
   GameObject[] typeor = Manager.singleton.arrayTen;
  dosomething();
}

我该如何重构它,比如只使用一个函数?

【问题讨论】:

  • 使用一组数组 GameObject[][] 然后访问 Manager.singleton.arrays[x] 其中 x 是你可以让按钮传递到你的方法中的东西......取决于你如何设置它......如果你通过代码附加监听器你可以做到完全动态......

标签: c# unity3d refactoring


【解决方案1】:

你可以那样做

        public void onSelect(int selectedAmount){
   switch (selectedAmount)
        {
        case 1:
                GameObject[] typeor = Manager.singleton.arrayOne;

             break;
        case 2:
               GameObject[] typeor = Manager.singleton.arrayTwo;

            break;
        case 10:
               GameObject[] typeor = Manager.singleton.arrayTen;

            break;
//optional
        default:
               GameObject[] typeor = Manager.singleton.arrayTwo;

            break;
        }
}

并称它为onSelect(HOW MANY SELECTED);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2018-11-26
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多