TryGetGUIDAndLocalFileIdentifier
Gets the GUID and local file identifier of an object.
Read time 3 minutesLast updated 2 days ago
Definition
- Type: Method
- Namespace: UnityEditor
- Assembly: UnityEditor.CoreModule
TryGetGUIDAndLocalFileIdentifier(Object, string, long)
Gets the GUID and local file identifier of an object.
public static bool TryGetGUIDAndLocalFileIdentifier(Object obj, out string guid, out long localId)
Parameters
Returns
Type | Description |
|---|---|
| bool | True if the method found the GUID and local file identifier. Otherwise, false. |
Remarks
When Unity serializes a reference to an asset, it stores the GUID of the asset file and a file ID that identifies the object inside the file. If a project uses text serialization, you can edit this information by hand. For more information, refer to Editor Settings. A common use is moving C# scripts from a project into a DLL, while keeping the GameObjects that use those scripts intact. For example, a project contains a MonoBehaviour script, a scene, and a GameObject with the script attached. The serialized scene file contains something like this, reduced to the relevant parts:
/* example .unity Scene contents:--- !u!1 &65078845GameObject: m_Component: -component: {fileID : 65078850}--- !u!114 &65078850MonoBehaviour: m_Script: {fileID : 11500000, guid : 9cbd8cdf99d44b58972fbc7f6f38088f, type : 3}*/
using System.Text;using UnityEngine;using UnityEditor;class ShowAssetIds{ [MenuItem("Assets/Show Asset Ids")] static void MenuShowIds() { var stringBuilder = new StringBuilder(); foreach (var obj in AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(Selection.activeObject))) { string guid; long file; if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out guid, out file)) { stringBuilder.AppendFormat("Asset: " + obj.name + "\n EntityId: " + obj.GetEntityId() + "\n GUID: " + guid + "\n File ID: " + file); } } Debug.Log(stringBuilder.ToString()); }}
Additional Resources: GlobalObjectId.
TryGetGUIDAndLocalFileIdentifier(int, string, long)
Gets the GUID and local file identifier of an object, using obsolete parameter types.
public static bool TryGetGUIDAndLocalFileIdentifier(int instanceID, out string guid, out long localId)
Parameters
Returns
Type | Description |
|---|---|
| bool | True if the method found the GUID and local file identifier. Otherwise, false. |
Remarks
These overloads are obsolete. Local file identifiers can be longer than 32 bits, for example for prefabs, so use an overload that takes a for and an EntityId or Object for the object.
longlocalIdTryGetGUIDAndLocalFileIdentifier(EntityId, string, long)
Gets the GUID and local file identifier of the object with the specified EntityId.
public static bool TryGetGUIDAndLocalFileIdentifier(EntityId entityId, out string guid, out long localId)
Parameters
Returns
Type | Description |
|---|---|
| bool | True if the method found the GUID and local file identifier. Otherwise, false. |
Remarks
When Unity serializes a reference to an asset, it stores the GUID of the asset file and a file ID that identifies the object inside the file. Use this method to get both values for an object. For a full explanation and example, refer to AssetDatabase.TryGetGUIDAndLocalFileIdentifier.
TryGetGUIDAndLocalFileIdentifier<T>(LazyLoadReference<T>, string, long)
Gets the GUID and local file identifier of the asset that a lazy-load reference points to.
public static bool TryGetGUIDAndLocalFileIdentifier<T>(LazyLoadReference<T> assetRef, out string guid, out long localId) where T : Object
Parameters
The lazy-load reference to the asset.
When the method returns, contains the GUID of the asset file.
When the method returns, contains the local file identifier of the object.
Returns
Type | Description |
|---|---|
| bool | True if the method found the GUID and local file identifier. Otherwise, false. |
Remarks
Use this overload to get the GUID and local file identifier without loading the referenced asset. For a full explanation, refer to AssetDatabase.TryGetGUIDAndLocalFileIdentifier.