Huxii
June 12, 2020, 2:27pm
1
Hello,
I would like to find all Models (in Editor), whose mesh-name differs from its file name. I can access the mesh name, but I am struggling with getting the name oft its file name.
How can I get it?
I did a research on google and did not find a lot to this topic. There was even an article, where was said, that it’s not possible.
In another thread I found this:
private static void GetFiles()
{
string[] files = Directory.GetFiles("Assets/Models", "*.*");
foreach (string sourceFile in files)
{
string fileName = Path.GetFileName("Assets/Models");
Debug.Log("fileName");
}
}
But this gives a list of all files in an directory and so not what I need.
I need something, where I can allocate the file of an specific mesh, like:
if (gameObject.sharedMesh.name != gameObject.file.name)
Unfortunately in the Scripting API I also did not find anything. Probably because I don’t know, how to search for this. I was searching for file, of course, but it doesn’t includes anything helpful as far as I can say. Then I thought that there will be something under Object and Mesh, but also did not find anything. So, I don’t know how to do it.
Thank you
Huxi
using UnityEngine;
using UnityEditor;
using System;
// To put in an Editor folder
public class CheckIfMeshNameMatchesWithGameObjectAssetsUtility
{
[MenuItem("Tools/Check if mesh' name matches gameObject name")]
static void CheckIfMeshNameMatchesWithGameObjectAssets()
{
string[] guids = AssetDatabase.FindAssets("t:Model");
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
GameObject model = AssetDatabase.LoadAssetAtPath<GameObject>(path);
CheckIfMeshNameMatchesWithGameObject(model);
}
}
private static void CheckIfMeshNameMatchesWithGameObject(GameObject model)
{
CheckMeshFilter(model);
CheckSkinnedMeshRenderer(model);
foreach (Transform child in model.transform)
CheckIfMeshNameMatchesWithGameObject(child.gameObject);
}
private static void CheckMeshFilter(GameObject model)
{
MeshFilter meshFilter = model.GetComponent<MeshFilter>();
if (meshFilter != null)
{
if (!meshFilter.sharedMesh.name.Equals(model.name, StringComparison.OrdinalIgnoreCase))
Debug.LogWarning($"Mesh & gameObject don't have the same name: {model.name} | {meshFilter.sharedMesh.name}", model);
else
Debug.Log($"Mesh & gameObject share the same name: {model.name}");
}
}
private static void CheckSkinnedMeshRenderer(GameObject model)
{
SkinnedMeshRenderer skinnedMeshRenderer = model.GetComponent<SkinnedMeshRenderer>();
if (skinnedMeshRenderer != null)
{
if (!skinnedMeshRenderer.sharedMesh.name.Equals(model.name, StringComparison.OrdinalIgnoreCase))
Debug.LogWarning($"Mesh & gameObject don't have the same name: {model.name} | {skinnedMeshRenderer.sharedMesh.name}", model);
else
Debug.Log($"Mesh & gameObject share the same name: {model.name}");
}
}
}
Huxii
June 12, 2020, 5:44pm
3
Hello @Hellium ,
Thank you very much again. It helped me a lot!
I adjusted the script so that it fits to my code base and it works
Here’s the adjusted code (if it’s of interest, I don’t know):
private bool FilterbyModelVsMeshName(GameObject gameObject)
{
Renderer renderer = gameObject.GetComponent<Renderer>();
MeshFilter filter = gameObject.GetComponent<MeshFilter>();
if ((renderer != null && filter != null) && filter.sharedMesh != null)
{
string path = AssetDatabase.GetAssetPath(filter.sharedMesh);
GameObject model = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (!filter.sharedMesh.name.Equals(model.name))
{
return true;
}
}
return false;
}
You’re the best!!!
Huxi