What is UnityEditor callback (if any) which is called when I press Ctrl+R in Editor to recompile changed sources?
As of Unity 2019 there are several options:
-
OnPostprocessAllAssets on an AssetPostprocessor fires when Ctrl+R finds an asset change, including added/deleted sprites, or code changes which trigger a compile.
using UnityEditor;
using UnityEngine;public class GeneralPostprocessor : AssetPostprocessor {
static void OnPostprocessAllAssets(string importedAssets, string deletedAssets, string movedAssets, string movedFromAssetPaths) {
Debug.Log(“OnPostprocessAllAssets fired”);
}
} -
DidReloadScripts fires after Ctrl+R triggers a compile, and also when the Play button is hit. It doesn’t need to be placed in an AssetPostprocessor subclass.
[UnityEditor.Callbacks.DidReloadScripts]
private static void OnScriptsReloaded() {
Debug.Log(“OnScriptsReloaded”);
} -
InitializeOnLoad causes a static constructor to be called after assemblies are reloaded, which is the same behavior as DidReloadScripts (on compile and on play).
[InitializeOnLoad]
public class SomeClass {
static SomeClass() {
Debug.Log(“SomeClass static constructor”);
}
}