状态改变法

发布于 2024-12-02 05:41:02 字数 466 浏览 1 评论 0原文

这只是一个简单的菜鸟问题... 我经常使用类似这样的东西(代码)来更改我的 GUI,所以我的问题是是否有比使用 bool 变量更有用的东西?

谢谢!

//Unity3D - C#

public class GuiBehaviour : MonoBehaviour
{
  private bool lookInside = false;

  void OnGUI ()
  {
    if (!lookInside) {
        if (GUILayout.Button ("Look Inside")) {
            lookInside = true;
        }
    } else {
        if (GUILayout.Button ("Exit View")) {
            lookInside = false;
        }
      }
  }
}

It's just a simple noobie question...
I often use something like this(code) to change my GUI, so my question is if there's something more useful than using bool variables?

Thanks!

//Unity3D - C#

public class GuiBehaviour : MonoBehaviour
{
  private bool lookInside = false;

  void OnGUI ()
  {
    if (!lookInside) {
        if (GUILayout.Button ("Look Inside")) {
            lookInside = true;
        }
    } else {
        if (GUILayout.Button ("Exit View")) {
            lookInside = false;
        }
      }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

人疚 2024-12-09 05:41:02

怎么样:

lookInside =!lookInside;

if(lookInside)
{
  GUILayout.Button ("Look Inside")
}
else
{
   GUILayout.Button ("Exit View")
}

what about:

lookInside =!lookInside;

if(lookInside)
{
  GUILayout.Button ("Look Inside")
}
else
{
   GUILayout.Button ("Exit View")
}
你丑哭了我 2024-12-09 05:41:02

对不同的按钮使用不同的处理程序,假设按钮仅在任一视图中可见。

private void InitializeComponent()
{
        lookInsideButton = new System.Windows.Forms.Button();
        lookInsideButton.Click += new EventHandler(lookInsideButton_Click);

        exitViewButton = new System.Windows.Forms.Button();
        exitViewButton.Click += new EventHandler(exitViewButton_Click);
}

void lookInsideButton_Click(object sender, EventArgs e)
{
    ShowInsideView();
}

void exitViewButton_Click(object sender, EventArgs e)
{
    ExitInsideView();
}

System.Windows.Forms.Button lookInsideButton, exitViewButton;

Use different handlers for different buttons, assumes buttons are only visible in either view.

private void InitializeComponent()
{
        lookInsideButton = new System.Windows.Forms.Button();
        lookInsideButton.Click += new EventHandler(lookInsideButton_Click);

        exitViewButton = new System.Windows.Forms.Button();
        exitViewButton.Click += new EventHandler(exitViewButton_Click);
}

void lookInsideButton_Click(object sender, EventArgs e)
{
    ShowInsideView();
}

void exitViewButton_Click(object sender, EventArgs e)
{
    ExitInsideView();
}

System.Windows.Forms.Button lookInsideButton, exitViewButton;
若沐 2024-12-09 05:41:02

我使用枚举是为了更好的可读性并拥有两个以上的状态。例如:

public class GuiBehaviour : MonoBehaviour
{
  private GUIState CurrentState;
  enum GUIState
  {
    LookInside,
    ExitView,
    GameOverView
  }

  void OnGUI(GUIState state)
  {
    CurrentState = state;
    switch(state)
    {
       case GUIState.LookInside:
         GUILayout.Button("Look Inside");
         break;
       case GUIState.ExitView:
         GUILayout.Button("Exit View");
         break;
       case GUIState.LookInside:
         GUILayout.Button("Game over");
         break;
     }
  }
}

I use enum for better readability and to have more than 2 states. For example:

public class GuiBehaviour : MonoBehaviour
{
  private GUIState CurrentState;
  enum GUIState
  {
    LookInside,
    ExitView,
    GameOverView
  }

  void OnGUI(GUIState state)
  {
    CurrentState = state;
    switch(state)
    {
       case GUIState.LookInside:
         GUILayout.Button("Look Inside");
         break;
       case GUIState.ExitView:
         GUILayout.Button("Exit View");
         break;
       case GUIState.LookInside:
         GUILayout.Button("Game over");
         break;
     }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文