Legacy Documentation: Version 2018.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

AssetDatabase.TryGetGUIDAndLocalFileIdentifier

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static method TryGetGUIDAndLocalFileIdentifier(obj: Object, out guid: string, out localId: int): bool;
public static bool TryGetGUIDAndLocalFileIdentifier(Object obj, out string guid, out int localId);
public static method TryGetGUIDAndLocalFileIdentifier(instanceID: int, out guid: string, out localId: int): bool;
public static bool TryGetGUIDAndLocalFileIdentifier(int instanceID, out string guid, out int localId);

Parameters

instanceIDInstanceID of the object to retrieve information for.
objThe object to retrieve GUID and File Id for.
guidThe GUID of the asset.
localIdThe local file identifier of this asset.

Returns

bool True if the guid and file id were successfully found, false if not.

Description

Get the GUID and local file id from an object instance id.

When Unity serializes an asset reference it points to two things: the GUID and file ID. GUID is a unique hash, and file ID is a value relative to the asset. Both of these values are used when a serialized asset references another asset.

If working with a text serialized project (see Editor Settings) it is possible to manually modify this information. A common use is moving C# script files from a project to a DLL while keeping any GameObjects using these scripts intact. As an example, suppose your project contains a C# MonoBehaviour, a Scene, and a GameObject with this script attached. When serialized the Unity scene file will contain something that looks like this (reduced to the relevant parts):

no example available in JavaScript
/* example .unity scene contents:

--- !u!1 &65078845 GameObject: m_Component: -component: {fileID : 65078850} --- !u!114 &65078850 MonoBehaviour: m_Script: {fileID : 11500000, guid : 9cbd8cdf99d44b58972fbc7f6f38088f, type : 3}

*/

Currently the m_Script field is pointing to the MyScript.cs GUID and file ID.

Now suppose you want to compile MyScript.cs to a DLL and remove MyScript from your project (since it's already compiled in a DLL). Removing MyScript.cs will break any MonoBehaviour references even though it still exists in DLL form. That is because the GUID and FileId of the MyScript behaviour are no longer the same. To fix this state, use TryGetGUIDAndLocalFileIdentifier with the DLL to retrieve the new asset ids, then find and replace the m_Script references to the old MyScript.cs.

no example available in JavaScript
using System.Text;
using System.Collections;
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; int file;

if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out guid, out file)) { stringBuilder.AppendFormat("Asset: " + obj.name + "\n Instance ID: " + obj.GetInstanceID() + "\n GUID: " + guid + "\n File ID: " + file); } }

Debug.Log(stringBuilder.ToString()); } }

Did you find this page useful? Please give it a rating: