I was wondering if I could have a MonoBehaviour listen to an event, that would get fired when its gameObject gets renamed.
First, I have to detect that ‘something’ is being renamed. And then get the selected gameObject from Selection - to get to the GO that’s being renamed, and maybe then send a msg to its children (or raise an event) - where my MB would be sitting there listening for that signal.
I was just checking out Event.current.commandName, like so (just like in the doc’s example):
void OnGUI()
{
Event e = Event.current;
if (e.commandName != "")
print(e.commandName);
}
But, it seems that the only thing it’s catching is “UndoRedoPerformed” - If I make a Copy, Paste, Duplicate, etc. It’s not detecting those.
Any ideas how to catch that Rename?
PS: I only care about this in the editor, not at runtime.
You could have an editor window register for these callbacks and then perform the required actions. You might also consider having a static class [InitializeOnLoad], then register for the callback if such functionality is needed constantly.
This is possible although a bit awkward.
If you make a class inherit from AssetPostprocessor,
when you rename an object, OnPostprocessAllAssets will be called twice.
The first time will have the new name of the file (or files in case you changed a folder) in movedAssets and the old name in movedFromAssetPaths. This also happens on move, not just on rename but with quite a simple path check you should be able to discern the two.
The second time will have the new object in importedAssets (again, this will happen for every imported object so it’s less useful for you). Hope that helps!