Post

Tackling Auto-Refresh Frustrations in Unity

Working on a large Unity project can be a pain for a plethora of reasons. One of them is the fact that Unity does recompile the project every time you tweak something externally and switch back to the Editor. This is super handy for those bite-sized projects, but for the behemoths? It could be a huge time-waster.

Disabling Auto-Refresh

Here’s a ray of sunshine though: Unity offers an “off” switch for this hyperactive behavior. You can do it by going to Edit -> Preferences -> General and unchecking the Auto Refresh checkbox.

General: Auto Refresh

For Unity 2021.2+, this setting has found a new home under Edit -> Project Settings -> General -> Asset Pipeline.

Pipeline: Auto Refresh

This will disable the automatic assets refreshing (and recompilation of the whole project). Now, when you flip back to the Editor, you can proceed without any interruptions. To update the project manually and recompile it with recent changes, you can use Assets -> Refresh from the main menu or corresponding Ctrl + R hotkey.

Assets: Auto Refresh

The only downside? You now have to remember to refresh the project manually. And believe me, you will forget about it very often. Pressing Play and trying to figure out why those changes are not applied… Great news, everyone! We traded in one flavor of time-wasting for another. Progress!

Custom Editor Script to the Rescue

Now, let’s improve our improvement. What if we’ll detect when that Play button is pressed and refresh the project automatically? Sounds like a good job for a custom editor script.

1
2
3
4
5
6
namespace EditorTools
{
    public class PlayModeAutoRefresher
    {
    }
}

Let’s add the [InitializeOnLoad] attribute to ensure our class gets up and running when the Unity editor loads. We’ll then add a static constructor to subscribe to the Play mode event.

1
2
3
4
5
6
7
8
[InitializeOnLoad]
public static class PlayModeAutoRefresher
{
    static PlayModeAutoRefresher()
    {
        // TODO: subscribe to the play mode state change
    }
}

According to Unity docs, we can use EditorApplication.playModeStateChanged event to detect when the play mode state changes. This event is triggered when the Editor enters, exits, or is about to enter or exit play mode.

1
2
3
4
5
6
7
8
9
static PlayModeAutoRefresher()
{
    EditorApplication.playModeStateChanged += OnPlayModeChanged;
}

private static void OnPlayModeChanged(PlayModeStateChange state)
{
    // TODO: refresh the project when entering the play mode
}

The callback function receives a PlayModeStateChange enum value that indicates the current play mode state. The ExitingEditMode state seems to be the perfect candidate for our needs.

1
2
3
4
5
6
7
private static void OnPlayModeChanged(PlayModeStateChange state)
{
    if (state == PlayModeStateChange.ExitingEditMode)
    {
        // TODO: refresh the project here
    }
}

To refresh the project, we can use the AssetDatabase.Refresh. This method will refresh the project manually and recompile it with recent changes.

1
2
3
4
5
6
7
private static void OnPlayModeChanged(PlayModeStateChange state)
{
    if (state == PlayModeStateChange.ExitingEditMode)
    {
        AssetDatabase.Refresh();
    }
}

All set! Moving forward, whenever you hit the Play button, your project will refresh and recompile automatically. Wave farewell to needless delays and pointless time-wasting. Welcome to a smoother development experience! (and the inevitable debugging that follows)

Final Version of the Editor Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using UnityEditor;

namespace EditorTools
{
    [InitializeOnLoad]
    public static class PlayModeAutoRefresher
    {
        static PlayModeAutoRefresher()
        {
            EditorApplication.playModeStateChanged += OnPlayModeChanged;
        }

        private static void OnPlayModeChanged(PlayModeStateChange state)
        {
            if (state == PlayModeStateChange.ExitingEditMode)
            {
                AssetDatabase.Refresh();
            }
        }
    }
}
This post is licensed under CC BY 4.0 by the author.