- Home /
Event or Callback for when Files are moved.
Is there an event or callback in Unity for when a file is moved? I want to pop up a little dialog box in the editor when a file is moved into the Resources folder and I was hoping i could just check for a Unity event or register for a Unity callback
I'm also interested in this. I'm keeping track of Resources via their GUID and right now my string paths can only be updated when their Editor is reopened (and they're stored on many objects...) after the Resource is moved.
Answer by AnomalusUndrdog · Nov 04, 2021 at 11:46 AM
Try AssetModificationProcessor.OnWillMoveAsset. The example code in that docs page should be enough to get you started. Note that just like what you see in the example code, you only need to implement OnWillMoveAsset
and the base class needs to be UnityEditor.AssetModificationProcessor
(don't remove the "UnityEditor."). No callback/event registration is necessary.
A few tips:
This gets called for any file move operation in the Project window. You'll have to use some string methods to check the asset being moved, like
destinationPath.Contains("/Resources/")
to check if something is being moved to a Resources folder, stuff like that.This also gets called when an asset is renamed. The path in both strings will be the same, just the filename will be different.