Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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:
    8
    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