Instant Enter Play Mode [Unity Essentials]

Every time you enter Play Mode in the Editor you are waiting like, what, five seconds? Why? :face_with_head_bandage:

Did you know that you can enter Play Mode instantaneously? :thinking:

Go to Project Settings => Editor and change the Enter Play Mode Settings so that Domain Reload is disabled. It looks like this in Unity 6:

Note that previous Unity versions have checkboxes where you had to confusingly check (enable) the box for Disable Domain Reload.

Random Pro Tip: avoid double negations! “Not disabled” is the same as “enabled”.
Guess which one our brains struggle to compute :face_with_monocle: and which is immediately obvious. :relieved:

Now entering Play Mode it’s practically instant!

There’s only one thing to consider henceforth:

static fields in runtime scripts will retain their value/reference when exiting playmode. Thus you have to explicitly reset these to the expected default value.

In your own scripts, the catch-all way to do so is to implement a static method with the [InitializeOnLoadMethod] attribute in each script that has static fields, like this:

public static event Action OnEnterGameEvent;
private static float playTime = 0f;

[InitializeOnLoadMethod]
private static void ResetStaticFields()
{
    OnEnterGameEvent = null;
    playTime = 0f;
}

Note that the initial value assignment for playTime is not sufficient to reset its value! Field initializers get applied only during domain reload, eg when changing a script. But without a domain reload (eg repeatedly entering Play Mode) the playTime field must be reset manually.

This may be confusing as it can lead to “sometimes it works, sometimes it doesn’t” issues - just something to keep in your mind. Otherwise, it’s just a little bit of discipline and enter Play Mode is instant! Well worth it! I have it enabled in every project! :hugs:

One caveat remains: some scripts (possibly AI generated) and some (non-maintained?) assets may not be compatible with disabled domain reload. You would have to test and if there are issues, fix them as mentioned above.

For more details refer to the Manual: Configurable Enter Play Mode

2 Likes