Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How is the GlobalObjectId for unpacked prefabs generated?

Discussion in 'Prefabs' started by Kleptine, Feb 8, 2021.

  1. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    274
    I'm working on some editor tooling that makes use of GlobalObjectId to store information about prefabs. Unfortunately, I've noticed that after a prefab is unpacked, all objects receive a different GlobalObjectId. This makes it incredibly difficult to build general editor tools with nested prefabs.

    However, this new id does seem deterministic. Regardless of how the object is unpacked, it will always receive the same 'Unpacked GlobalObjectId'.

    It would be extremely helpful to know this Unpacked GlobalObjectId is computed. Is it just a simple formula from the original PrefabInstanceId?

    Thanks!
     
  2. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    274
    Bump. Any help on this?

    I tried using the formula mentioned in another thread:
    ulong fileId = (id.targetObjectId ^ id.targetPrefabId) & 0x7fffffffffffffff;

    But unfortunately this doesn't seem to work. I have an object in a prefab with Editor GlobalObjectId:
    GlobalObjectId_V1-2-4307801f4eff63940bd266f73c9654c4-7432198799418510837-1864888600

    Using that formula returns this for the unpacked GID:
    GlobalObjectId_V1-2-4307801f4eff63940bd266f73c9654c4-7432198799835741421-0

    But the actual unpacked GID is:
    GlobalObjectId_V1-2-4307801f4eff63940bd266f73c9654c4-1864890185-0
     
  3. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    274
    I spent a day on this and narrowed down a minimal reproduction of what looks like a bug in the asset pipeline or prefab system. Tested on the latest 2020.2 and 2021.1 alpha.

    The formula mentioned above works correctly. If you have a Prefab Instance with GID
    GlobalObjectId_V1-2-4307801f4eff63940bd266f73c9654c4-7432198799418510837-1864888600

    when the prefab is unpacked, the unpacked object will have a GID of
    GlobalObjectId_V1-2-4307801f4eff63940bd266f73c9654c4-7432198799835741421-0

    except after a standalone build where the original Prefab Asset is packaged into the build. For example, if the Prefab Asset is in Resources/ or referenced directly.

    In this case, the Prefab Asset itself seems to become corrupted. After the build, whenever you unpack a Prefab Instance, the GlobalObjectId will not follow the above formula, and instead finds the next unused fileID in the scene. ie. The returned GlobalObjectId is:
    GlobalObjectId_V1-2-4307801f4eff63940bd266f73c9654c4-1864888605-0

    This doesn't look like a bug on the surface, but if you force re-import the prefab asset in question, the unpacked GlobalObjectId will revert back to using the XOR formula, and Unity will show the error "generated inconsistent result for asset (prefab_path)".

    This especially seems like a bug because these assets aren't changing at all. A re-import should have no affect on the GlobalObjectIds that are generated.

    Can anyone verify why this is happening? It breaks a lot of the editor tooling we've made. Currently our workaround is to automatically reimport the packed prefab assets after the build, but that's at best a bandaid solution.
     
    Last edited: Mar 9, 2021
  4. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Have you filed a bug report? Just started using GlobalObjectId and am concerned
     
  5. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    274
    I didn't file a bug report for this one -- was hoping to get someone on the forums to look into it and wasn't sure if it was a bug.

    For now we just reimport all the packed prefab assets after every build -- has been working fine for the last few months, at least, enough that I have no regrets using GlobalObjectId.