Get all the names of the models (Mesh) on the map and also their file path.

This is what I have done so far but I can not get the path of the files.

using UnityEngine;
using UnityEditor;


MeshFilter[] foundMeshObjects = GameObject.FindObjectsOfType(typeof(MeshFilter)) as MeshFilter[];

        foreach (var item in foundMeshObjects)
        {
            Debug.Log(item.gameObject.GetComponent<MeshFilter>().sharedMesh.name);
            Debug.Log(AssetDatabase.GetAssetPath(item.gameObject.GetComponent<MeshFilter>().mesh));
        }

Why do you use sharedMesh in the first case but mesh in the second? using mesh would create a duplicate for this renderer so it’s detached from the asset it came from.

It’s just a Log.
In any case it does not work.

What do you mean? you pass “mesh” into “GetAssetPath” but mesh is a duplicate and not the asset, so you don’t get an asset path. You haven’t even said what you actually see in the log. Do you get no log at all? do you get a log but the path is empty? Maybe the object you’re looking for in your scene does not have a MeshFilter but is a skinned mesh and therefore has a SkinnedMeshRenderer?

I just pulled up my test scene, quickly added a static mesh (with meshfilter) and a skinned mesh (with a SkinnedMeshRenderer) to my scene and used this script:

        var foundMeshFilters = GameObject.FindObjectsOfType<MeshFilter>();
        foreach (var item in foundMeshFilters)
        {
            Debug.Log("MF: " + item.sharedMesh.name + " = " + UnityEditor.AssetDatabase.GetAssetPath(item.sharedMesh));
        }

        var foundSMRs = GameObject.FindObjectsOfType<SkinnedMeshRenderer>();
        foreach (var item in foundSMRs)
        {
            Debug.Log("SMR: " + item.sharedMesh.name + " = " + UnityEditor.AssetDatabase.GetAssetPath(item.sharedMesh));
        }

I get two logs which look like this:

MF: high_6_sides = Assets/Dice/Models/dice_high.FBX
SMR: Mesh = Assets/Spider Green/Spider_model/SPIDER.fbx

The first mesh is a 6 sided die which has a MeshFilter. The second mesh is an animated spider model (both from the assetstore). So it should work just fine.

However when I use mesh instead of sharedMesh inside the GetAssetPath method, I get this

MF: high_6_sides =
SMR: Mesh =

because, as I said, mesh returns a copy of the mesh which is not an asset on disk but just a copy in memory. So it does not have an asset path.

1 Like