JetBrains / resharper-unity

Unity support for both ReSharper and Rider
Apache License 2.0
1.21k stars 134 forks source link

An action to restart the game in Unity #618

Open oxysoft opened 6 years ago

oxysoft commented 6 years ago

Something that can be mapped to a shortcut. Here is how I implemented in Unity using a short editor script..

public static class ReplayGameMenuItem
{
private static bool isDisabled;

[MenuItem("Tools/Re-play Game _F5")]
public static void MenuItem()
{
    if (isDisabled)
        return;

    if (EditorApplication.isPlaying)
    {
        EditorApplication.isPlaying = false;
        EditorApplication.playModeStateChanged += StateChanged;
    } else
    {
        EditorApplication.isPlaying = true;
    }

    isDisabled = true;
}

private static void StateChanged(PlayModeStateChange change)
{
    if (change == PlayModeStateChange.EnteredEditMode)
    {
        EditorApplication.isPlaying = true;
        isDisabled = false;
        EditorApplication.playModeStateChanged -= StateChanged;
    }
}

}

van800 commented 6 years ago

So, if we are in Play mode, calling this action stops Play, waits till entering EditMode and Plays again?