Detect when a [ExecuteAlways] component has been removed

I have an editor tool, consisting of a MonoBehaviour with the [ExecuteAlways] attribute. The tool creates some meshes, which (in order to not disappear on reopen when the tool is used in a prefab) have to be saved to an asset on disk. When the MonoBehaviour script is removed from a GameObject or when the whole GameObject it’s on is deleted, I would like to run some cleanup logic, which removes the mesh assets, as they’re not supposed to be used anywhere else.

Here comes the problem: there doesn’t seem to be any callback for this. From what I’ve found, the OnDestroy() event is supposed to be used for such purposes, but it doesn’t mix well with [ExecuteAlways], as it gets called every time the prefab/scene the MonoBehaviour is in gets closed. If I delete the mesh assets every time the prefab gets closed, I haven’t really accomplished anything.

How would I go about accomplishing something like this?

Normally I would point to the ObjectChangeEvent API: Unity - Scripting API: ObjectChangeEventStream

Though there doesn’t seem to be an event to match this.

But in all honesty, this might be better served by making an editor window or a custom inspector for this. [ExecuteAlways] doesn’t make for the best comprehensive editor tooling.

I ended up going with the solution mentioned here. Deciding whether to run the code in OnDestroy() or not, depending on the command of the current event in editor’s event bus.

well to quote the creation of beginnings, u cant destroy what you have not created. if u want full trace post the error log