- Home /
Quit application before relaunching to specific scene
I am making a game where, if you take too long to complete the game, the universe begins to fall apart and the game "crashes" (the storyline is a little too complex and unnecessary to explain here). I will simulate the crash by setting time.timeScale to 0 to freeze everything, and then use close the application. I can use time.realtimeSinceStartup to measure time while everything is frozen. Anyway, after quitting, I want to be able to relaunch the application to a specific scene (in this case, an alternate universe). Is there a way to do this?
Answer by fdz_ · Dec 09, 2017 at 10:06 PM
Basically you are asking how to save player's progress. One way is with xml/json, the other (and the simplest way) is using PlayerPrefs
If you like videos, here's playlist by Board To Bits Games explaining different ways it's possible to save game data, plus, a little example of how to do what you want with PlayerPrefs:
public bool fakeTheCrash;
public string normalUniverseSceneName;
public string alternativeUniverseSceneName;
void Start()
{
int prefs_fakeCrash = PlayerPrefs.GetInt( "fakeCrash" );// using GetInt() because PlayerPrefs doesn't support Booleans
if ( prefs_fakeCrash == 0 ) {// false
UnityEngine.SceneManagement.SceneManager.LoadScene( normalUniverseSceneName );
}
else if ( prefs_fakeCrash == 1 ) {// true
UnityEngine.SceneManagement.SceneManager.LoadScene( alternativeUniverseSceneName );
}
}
private void Update()
{
if ( fakeTheCrash ) {
fakeTheCrash = false;
PlayerPrefs.SetInt( "fakeCrash", 1 );
}
}
Thanks for re$$anonymous$$ding me to save the player's data before "crashing". That would be helpful. But this doesn't answer my question about how to actually quit the application before relaunching it automatically.
So... yeah... I had some fun, and now I know how to do this on windows with the cmd.
Hopefully it may point you on the right direction if you need to do it on other platforms.
I made 3 scripts. Each one should be on a different scene.
Script in charge of loading the scenes:
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
public class SceneChooser : $$anonymous$$onoBehaviour
{
bool loadAlternativeWorld=false;
private void Start()
{
loadAlternativeWorld = PlayerPrefs.GetInt( "loadAlternativeWorld" ) == 1;
if ( loadAlternativeWorld )
Scene$$anonymous$$anager.LoadScene( "Alternative World" );
else
Scene$$anonymous$$anager.LoadScene( "Normal World" );
}
}
The script for the Normal Scene (with the cmd stuff)
using UnityEngine;
using System.IO;
using System.Diagnostics;
public class NormalWorld : $$anonymous$$onoBehaviour
{
public string exeName="";
void Update ()
{
if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) ) {
PlayerPrefs.SetInt( "loadAlternativeWorld", 1 );
}
}
private void OnApplicationQuit()
{
if ( PlayerPrefs.GetInt( "loadAlternativeWorld" ) == 1 ) {
var command1 = "TI$$anonymous$$EOUT /T 3 /NOBREA$$anonymous$$";
var command2 = Path.GetDirectoryName( Application.dataPath ) + "/" + exeName + ".exe";
//var command3 = "pause >nul";
ExecuteCommand( command1 + " & " + command2 );
}
}
void ExecuteCommand( string command )
{
Process process = new Process();
// redirect the output stream of the child process.
//process.StartInfo.UseShellExecute = false;
//process.StartInfo.RedirectStandardOutput = true;
//process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c" + command;
process.Start();
//var output = process.StandardOutput.ReadToEnd();
//process.WaitForExit();
//var exitCode = process.ExitCode;x
//process.Dispose();
//process = null;
}
}
And finally, the script for the Alternative Scene
using UnityEngine;
public class AlternativeWorld : $$anonymous$$onoBehaviour
{
void Update ()
{
if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) ) {
PlayerPrefs.SetInt( "loadAlternativeWorld", 0 );
}
}
}
Sources from stackoverflow:
Execute multiple command lines with the same process using .NET
C# console app can't run a batch file but batch file works fine in command prompt
I accepted this answer because it does answer my question, however I decided that it would work better to just freeze the game, make the screen pitch black, and then load a different scene, ins$$anonymous$$d of using the command line, which would be inefficient because I want my game to work on $$anonymous$$ac as well as PC.
That's good. That seams more reasonable than shutting and reopening the game.
Though... if you really wanted i think you could use a bash script.
How make simple bash script mac
Your answer
Follow this Question
Related Questions
Game Crashes On Startup? 3 Answers
Game App Crashes when Application.Quit() 0 Answers
Troubleshoot a Browser Based Game Crash 1 Answer