17.6 C
New York
Monday, April 28, 2025

Unit: You can not discover a persistent identification between the editor’s periods for an object


Once I Discover “Persistent Unity ID”The primary result’s the Documentation for unityeditor.globalobjectidThat appears to do precisely what you need:

Distinctive and steady venture identifier for objects in scenes and different varieties of property to be used on the time of authority.

Use this construction to acquire distinctive identifiers to things which might be a part of scenes or inside different property, akin to prefabricated and scriptablebjects. The identifiers are steady and chronic in all enhancing periods and knowledge serialization operations, akin to altering the energetic scene and financial savings and recharge.

We are able to acquire a serializable model of this identification for an object of the scene, prefabricated or son of a prefabrication with:

string id = UnityEditor.GlobalObjectId.GetGlobalObjectIdSlow(gameObject).ToString();

This chain is exclusive and steady by way of the mode of replica by getting into/leaving and the closure/reopening of the editor. This additionally works for different varieties of property, or to consult with particular person parts within the object, solely commerce gameObject For acceptable reference.

For objects in an open scene, the one warning is that you just want Save the scene at the least as soon as After the thing is created, earlier than a world ID is assigned.

To investigate such a serialized chain in an ID, you need to use:

UnityEditor.GlobalObjectId.TryParse(stringId, out var idStruct);

Then you could find the thing with this identification with:

var object = UnityEditor.GlobalObjectID.GlobalObjectIdentifierToObjectSlow(idStruct);

Or if you happen to solely want your occasion identification:

int instanceID = UnityEditor.GlobalObjectID.GlobalObjectIdentifierToInstanceIDSlow(idStruct);

These will fail (returning null or zero, respectively) if the thing you’re on the lookout for is in a scene that isn’t presently loaded, or if it can’t be discovered (for instance, the asset or entrance was eradicated was malformed).

There are additionally matrix variations of every of those that may work in numerous ID on the identical time. This helps amortize the price of efficiency in a number of operations, since, as indicated by the names of strategies, they’re “gradual”, the worth we pay for the individuality and assured international stability.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles