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 &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()); } }
See Also: GlobalObjectId
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.