Search code examples
c#unity-game-engineunity3d-editor

How to retrieve fileID and Guid used in a prefab directly?


is there any method that can return the fileID and corresponding guid of m_Mesh property in a prefab file? The m_Mesh is the mesh that used by the meshfilter component.

How can i get the two values, 4300000 and 8b73e8872ca76104bbca4ee2b704a1b4 via script?

enter image description here


Solution

  • Adding to this answer I think in order to get the according mesh asset you can go through the SerializedObject something like e.g.

    var meshFilter = GetComponent<MeshFilter>();
    var so = new SerializedObject(meshFilter);
    so.Update();
    var meshProperty = so.FindProperty("m_Mesh");
    var obj = meshProperty.objectReferenceValue;
    

    Now from here you can forward this into the given

    if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out var guid, out long file))
    {
        Debug.Log(
            $"Asset: {obj.name}\r\n" +
            $"Instance ID: {obj.GetInstanceID()}\r\n" +
            $"GUID: {guid}\r\n" +
            $"File ID: {file}"
        );
    }