Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Feature Request: More UnityEditor Callbacks for prefab instance lifecycle management

Discussion in 'Editor & General Support' started by awiebe, Aug 10, 2020.

  1. awiebe


    Oct 16, 2018
    I have been scratching my head a few days now trying to come up with a stable way to determine if an object was duplicated by the editor.

    It seems the best we have right now is

    Which is good enough to uniquely identify an object as long as it doesn't move scenes. That's great except I want to be able to move objects between scenes without my logic breaking.

    I am trying to programmatically assign value that I don't want shared when I copy an object in the inspector.
    (You guys could also use this kind of logic internally so that duplicating a terrain object doesn't share the terrain data asset, with undesirable results)

    Right now it only seems possible to detect object duplication on a per scene basis.

    using UnityEditor;
    using UnityEngine;
    class EditorTrackedObject : MonoBehaviour {

    [HideInInspector] public string editorGUID = null;
    void Awake () {
    if (editorGUID == null) {
    //New object
    editorGUID = GlobalObjectId.GetGlobalObjectIdSlow (this);
    } else if (GlobalObjectId.GetGlobalObjectIdSlow (this) != editorGUID) {
    //Object duplicated programmatically or through editor command
    //Generate new instance variables that should be unique across duplication

    void ObjectWasMovedBetweenEditorScenes() {
    //I need some way for Unity to tell me that this object traveled, that or a truly globally unique ID,
    // but I don't think that fits into Unity's current instance serialization scheme.

    //NOt a new object, our GUID just changed so update it.
    editorGUID = GlobalObjectId.GetGlobalObjectIdSlow (this);

    Last edited: Aug 10, 2020