AssetDatabase.TryGetGUIDAndLocalFileIdentifier

Switch to Manual
Obsolete public static bool TryGetGUIDAndLocalFileIdentifier (Object obj, out string guid, out int localId);
Obsolete public static bool TryGetGUIDAndLocalFileIdentifier (int instanceID, out string guid, out int localId);
public static bool TryGetGUIDAndLocalFileIdentifier (Object obj, out string guid, out long localId);
public static bool TryGetGUIDAndLocalFileIdentifier (int instanceID, out string guid, out long localId);

Parameters

instanceID要检索其信息的对象的实例 ID。
obj要检索其 GUID 和文件 ID 的对象。
guid资源的 GUID。
localId该资源的本地文件标识符。

Returns

bool 如果成功找到 GUID 和文件 ID,则返回 true;否则,返回 false。

Description

Warning Use the overload with a long localId parameter. Using the overload with an integer localId parameter can cause an integer overflow in localId. This can happen when the object passed to the API is part of a Prefab.

从对象实例 ID 中获取 GUID 和本地文件 ID。

当 Unity 序列化资源引用时,此函数会指向 GUID 和文件 ID。GUID 是唯一哈希值,文件 ID 是相对于资源的值。当序列化资源引用另一资源时,会使用这两个值。

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):

/* 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}

*/
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 Instance ID: " + obj.GetInstanceID() + "\n GUID: " + guid + "\n File ID: " + file); } }

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