Can I create code that triggers every time a new prefab is dragged into the scene in the editor?

Hello there, I’m having trouble with something recently. I have a graphics package with prefabs like chairs, beds, drawers etc… that use materials within it’s own package. I thought that it would be good if I created a script that would generate unique materials (by copying the materials of the prefab into a scenes folder in the project folder) for the dragged in prefab, so that I could easily have these objects have different materials and textures.

The first step to this though I would think would be to find when the script would trigger. I’m wondering if it’s possible to trigger an editor script when a new prefab is dragged into the scene within the editor?

EditorApplication.hierarchyWindowChanged sends various events when something in the hierarchy changed.

Another idea I have is to attach a custom MonoBehaviour to your prefabs, which can be automated using an AssetPostprocessor.

The custom MonoBehaviour uses the ExecuteInEditMode attribute, thus gets an Awake() call when you drag it into the scene. You can use the Awake() method in your custom Component to do whatever you need then.

You can use use Application.isPlaying to distinguish whether the Awake() call is made during edit-mode.

You can use hideFlags to make sure the Component is used in the editor only.

1 Like