statechanging method
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;
}开发者_如何学Go
}
}
}
what about:
lookInside =!lookInside;
if(lookInside)
{
GUILayout.Button ("Look Inside")
}
else
{
GUILayout.Button ("Exit View")
}
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;
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;
}
}
}
精彩评论