Get GUID of child asset

Hello!

I’ve been trying to get a GUID of a child asset, and sort of hit a wall. The reason I need this is to be able to save / load an unique id for each asset that never changes. (Unlike instant id).

  • AssetDatabase.AssetPathToGUID: requires an asset path, which is the same for both a parent and child asset.
  • AssetDatabase.TryGetGUIDAndLocalFileIdentifier: The guid is the same as above, but it also returns a nice local field identifier, which is different for each child asset.

The last one is interesting, since I could just combine the guid and the local file identifier to make a nice unique id. The problem is, there is seemingly no way to do it the other way around, aka use a guid+local file identifier to find an asset.

The only idea I have is to detect that the asset is a child asset, and then manually search for a matching local file identifier every time. Not exactly ideal, but passable if I have a functioning cache.

Any thoughts or ideas?

2 Likes

In Unity 2020.1, ObjectIdentifier gained a ToObject method, which lets you look up an object with a GUID/localId pair.

I don’t know of any way to do this before 2020.1. Since there aren’t usually that many sub-assets, you could use the GUID to load them all and then use AssetDatabase.TryGetGUIDAndLocalFileIdentifier on each.

2 Likes

Woah, cool! That’s perfect! For now I did what I mentioned, was check if the input guid and local id is a main asset, and if not manually going through all child assets until it finds it.

I’m super glad they’re adding better features for object identifications in editor, since it’s always been lacking. :smile: