Game Development Reference
In-Depth Information
It would be nice if the game would stop Play mode with the quit button when you are working in
Unity. It turns out you can add “editor” code to customize the Unity editing environment. While that
is generally beyond the scope of this topic, having Play mode stop is too valuable to pass up.
In the EndGameGUI script, change Application.Quit to the following:
5.
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
6.
Save the script.
7.
Turn off “Maximize on Play,” and click Play. Then click the Quit button in
the scene.
Play mode ends.
Now, of course, you will want to have the appropriate message appear when the game has ended.
Because the end message exists only as code, you will begin by creating a flag to suppress it until it
is triggered.
Add the following variable to the EndGameGUI script:
bool showEndGUI = false; // flag to hide/show end message and options
In the onGUI function, beneath the GUI.skin line, add
8.
if (showEndGUI) {
Add the closing curly bracket after the Quit conditional, and then indent the
contents for better readability:
9.
} // close end message conditional
Next you will create a little function that will turn on the flag and update the message according to
who calls it.
1.
Add the following function:
public void TriggerMessage (string results) {
finishedMessage = results; // set the correct message
showEndGUI = true; // turn on the message
}
2.
Save the script, and click Play to make sure the GUI no longer shows.
3.
Stop Play mode.
 
Search WWH ::




Custom Search