Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. 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

    awiebe

    Joined:
    Oct 16, 2018
    Posts:
    10
    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
    https://docs.unity3d.com/ScriptReference/GlobalObjectId.html

    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
unityunity