Version: 2021.1

AssetDatabase.TryGetGUIDAndLocalFileIdentifier

切换到手册
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);
public static bool TryGetGUIDAndLocalFileIdentifier (LazyLoadReference<T> assetRef, out string guid, out long localId);

参数

instanceID 要检索其信息的对象的实例 ID。
obj 要检索其 GUID 和文件 ID 的对象。
assetRef 要为其获取 GUID 和文件 ID 的资源引用。
guid The GUID of an asset.
localId 该资源的本地文件标识符。

返回

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

描述

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

Warning: Avoid the obsolete versions of this function, which use int for the localId parameter instead of long. Local Ids can be longer than 32 bits in some cases, such as for Prefabs. 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.

如果使用文本序列化项目(请参阅 Editor Settings),可能需要手动修改此信息。常见用途是,将 C# 脚本文件从项目移动到 DLL 的同时,让使用这些脚本的任何游戏对象保持不变。举例来说,假设您的项目包含一个 C# MonoBehaviour、一个场景以及一个附加该脚本的游戏对象。序列化时,Unity 场景文件将包含与下述相似的内容(简化为相关部分):

/* example .unity Scene contents:

--- !u!1 &amp;65078845 GameObject: m_Component: -component: {fileID : 65078850} --- !u!114 &amp;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()); } }