Run a script when prefab added to a scene or duplicated

This is really two questions in one, but very closely related.

I’ve been combing the Unity docs and various sites trying to find a method of running a script at design time either when the user adds a prefab into the scene or duplicates an existing prefab in the scene. I only need the script to run when the object comes into existence in the scene, not when it is loaded or instantiated by loading the scene or running the game.

The best I’ve come up with so far is to do something like the following:

[ExecuteInEditMode]
public class CreationResponder : MonoBehaviour
{
    public string creationGuid;

    void Awake()
    {
        // If a GUID hasn't been assigned in the editor yet, generate a unique
        // identifier for this object to be stored in the central system.
        if (Application.isEditor && creationGuid == string.Empty)
        {
            creationGuid = Guid.NewGuid().ToString();
        }
    }
}

I would love to hear that I missed something easy.

I like your approach, very clever, although I’m scared to death of code that uses ExecuteInEditMode. Keep in mind Awake() will also run on startup, although in your case that won’t matter.

I’m aware of nothing in Unity to support this (as of version 3.4.2, at least) I have the same issue instantiating prefabs using EditorUtility.InstantiatePrefab() from a custom EditorWindow.

my solution - Prefabs I create with my window must extend an interface with OnCreatedByEditor() called after InstantiatePrefab(). Great if you’re using an EditorWindow, but I don’t know a way to make it work if you drag prefabs from your Project view.