- Home /
Question by
skmr · May 02, 2020 at 08:36 AM ·
scripting problemmeshprefabexport
Export game objects with mesh paths
I want to write an exporter of scene game objects to a .txt file. The end format will be something like:
<gameobject name> <mesh path> <position>
for example:
PalmTree_01 ..\meshes\PalmTree.fbx (10.0, 0.0, 0.0)
PalmTree_02 ..\meshes\PalmTree.fbx (20.0, 0.0, 0.0)
Getting the game objects and positions is the easy part, but I am struggling with getting the mesh paths. This is what I tried:
void Start()
{
GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
foreach (GameObject go in allObjects)
{
var mfilter = go.GetComponent<MeshFilter>();
if (mfilter)
{
Debug.Log(AssetDatabase.GetAssetPath(mfilter.mesh);
}
}
}
But this returns a path to ".prefab" file rather than the fbx file with the mesh. I tried also this:
void Start()
{
GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
foreach (GameObject go in allObjects)
{
var mfilter = go.GetComponent<MeshFilter>();
if (mfilter)
{
var prefab = PrefabUtility.FindPrefabRoot(go);
var prefabMFilter = prefab.GetComponent<MeshFilter>();
if (prefabMFilter)
{
Debug.Log(AssetDatabase.GetAssetPath(prefabMFilter.mesh));
}
}
}
}
But once again this returns the path to prefab. How to get the path to the fbx file used in the prefab (let's assume the prefab will always contain only one meshfilter)?
Comment