- Home /
UnityEditor callback OnRefresh()?
What is UnityEditor callback (if any) which is called when I press Ctrl+R in Editor to recompile changed sources?
There are a handful of static variables which can we used as event callbacks within the EditorApplication class. I am not sure what cntl+r actually does, so you will want to try the ones that sound closest to the event you need to hook.
Answer by sarahnorthway · Jul 19, 2019 at 12:03 AM
As of Unity 2019 there are several options:
1) 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");
}
}
2) 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");
}
3) 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");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613114659im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Need help with editor (script?) 1 Answer
How can I preserve static object/data between editor and play? 2 Answers
Internal Compiler Error trying to use NSubstitute 1 Answer
Cloning GameObjects with custom inspector script attached 0 Answers