Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


globalobjectid

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
globalobjectid [2021/02/08 08:26]
uninomiconadmin
globalobjectid [2021/02/11 20:36] (current)
uninomiconadmin
Line 9: Line 9:
 //All notes below only apply to running the game in Play mode in the Editor.// //All notes below only apply to running the game in Play mode in the Editor.//
  
-Prefabs Instances have a GID value that is different from the GID assigned when the game is run in Play Mode. When a scene is loaded in play mode, prefabs are merged (unpacked) into standard game-objects. These gameobjects have a different GID than the original prefab. However the GID of the unpacked GameObjects is *still* unique and stable! +When a scene is loaded in play mode, prefabs are unpacked ((I've seen this mentioned in the profiler as Prefabs.MergePrefabs, during [[scenemanager]] scene loads.)) into standard game-objects. These gameobjects have a different GID than the original prefab. However the GID of the unpacked GameObjects is *still* unique and stable!
- +
-Whatever process that Unity uses to unpack gameobjects(( +
-I've seen this mentioned in the profiler as Prefabs.MergePrefabs, during [[engine:scenemanager]] scene loads. +
-)) must be determinstic, and generate a fileID for the new gameobject that is based on the prefab.+
  
 For example. You may have a prefab in the scene with GID: For example. You may have a prefab in the scene with GID:
Line 27: Line 23:
 ==== Converting a Prefab Gid to an Unpacked Gid ==== ==== Converting a Prefab Gid to an Unpacked Gid ====
  
-You can convert between the two using the following formula:((https://forum.unity.com/threads/how-is-fileid-generated-for-m_correspondingsourceobject.726704/#post-4851011)) +You can convert between the two using the following formula:(( 
 +Source: [[https://forum.unity.com/threads/how-is-fileid-generated-for-m_correspondingsourceobject.726704/#post-4851011]]  This is for a slightly different situation, but I have found that the same formula applies to unpacking prefabs 
 +))
  
 ''%%(%%prefabGid.targetObjectId ^ prefabGid.targetPrefabId) & 0x7fffffffffffffff'' ''%%(%%prefabGid.targetObjectId ^ prefabGid.targetPrefabId) & 0x7fffffffffffffff''
  
 +++++ Example Code|
 +
 +<code csharp>
 +private static GlobalObjectId ConvertPrefabGidToUnpackedGid(GlobalObjectId id)
 +{
 +    ulong fileId = (id.targetObjectId ^ id.targetPrefabId) & 0x7fffffffffffffff;
 +    bool success = GlobalObjectId.TryParse(
 +        $"GlobalObjectId_V1-{id.identifierType}-{id.assetGUID}-{fileId}-0",
 +        out GlobalObjectId unpackedGid);
 +    Assert.IsTrue(success);
 +    return unpackedGid;
 +}
 +</code>
 +
 +++++
 +
 +===== Bug: Querying deleted objects =====
 +When calling ''GlobalObjectId.GlobalObjectIdentifiersToObjectsSlow'' with a GlobalObjectId that was assigned to a now-deleted object, the Object returned will sometimes be a valid, arbitrary Object in the project, rather than the correct value of ''null''.
 +
 +For example, in this case, when passed an array of only scene objects:
 +{{ :hdmvjaumtf.png?direct&400 |}}
 +
 +The function will return non-scene objects (the shader editor asset at 205). 
 +{{ :is0bzbpnb6.png?direct&400 |}}
 +
 +This seems to be a bug. The correct result should have been ''null'', because that GID was deleted.
 +
 +I have had some luck <sup>[//[[:wiki:tag:dark-magic|dark magic]]//]</sup> querying these 'bad' GIDs individually with ''GlobalObjectIdentifierToObjectSlow''. This seems to properly return null in certain cases. This does not cure all cases, however((Luckily, there was only a small set of objects that were valid to be stored by my GIDs, and so I could discard any GIDs resolving to the wrong type.)).
 +
 +Update: This is fixed in 2021.1 [[https://issuetracker.unity3d.com/issues/globalobjectidglobalobjectidentifierstoobjectsslow-sometimes-returns-a-random-existing-gameobject|bug tracker]].
  
globalobjectid.1612772780.txt.gz · Last modified: 2021/02/08 08:26 by uninomiconadmin