'Unity - How to rename Mesh via script?

I am working on a script to rename Mesh in Unity directly because every time if I rename a FBX file the mesh asset still keep the old name as last time it has been exported from Maya of 3dsMax, which is really annoying. What I have tried all doesn't work, the code like:

public class Rename
{
    [MenuItem("Assets/Test")]
    private static void renameTest()
    {
        //select a fbx in project window
        GameObject fbx = Selection.activeGameObject;

        Mesh meshAsset = fbx.GetComponentInChildren<MeshFilter>().sharedMesh;

        meshAsset.name = "new name";
        Debug.Log(meshAsset);
    }
}

I also tried:

private static void renameTestv2()
{
    GameObject fbx = Selection.activeGameObject;

    Mesh meshAsset = fbx.GetComponentInChildren<MeshFilter>().sharedMesh;

    Mesh meshAsset2 = Object.Instantiate(meshAsset);
    meshAsset2.name = "new name";
    fbx.GetComponentInChildren<MeshFilter>().sharedMesh = meshAsset2;
}

The third way can change the Mesh's name on FBX's Meshfilter component, but the actual subasset still using the old one, you can find what I mean with the attached pictures.

private static void renameTest()
{
    GameObject fbx = Selection.activeGameObject;
    string path = AssetDatabase.GetAssetPath(fbx);
    Mesh meshAsset = AssetDatabase.LoadAllAssetRepresentationsAtPath(path)[0] as Mesh;
    meshAsset.name = "new name";
    fbx.GetComponentInChildren<MeshFilter>().sharedMesh = meshAsset;
    Debug.Log(meshAsset);

}

How can I solve this problem?

enter image description here

If I re-export FBX from DCC to change the Mesh's name, it will lost link with prefab but the guid of fbx file actually doesn't change, why does this happen? My steps are

  1. rename aa.FBX file in unity to bb.FBX
  2. Import bb.FBX into 3dsmax, and change the name from aa to bb in 3dsmax
  3. Export to unity as bb.FBX again.


Solution 1:[1]

I think this can't be done directly within Unity.

Reason: What you see in Unity in the assets is an already imported virtual model Prefab. Actually in your system file browser it is still a normal FBX file though and Unity doesn't directly write stuff into that FBX file. Unity is not a 3D modelling software and therefore there is no attempt to address this.

In other words from the FBX Exporter Package

When Unity imports a Model from a 3D modeling application such as Autodesk® Maya®, Unity creates a Model Prefab, which is a read-only representation of the FBX file's contents. You can't edit Model Prefabs in Unity, apart from changing the import settings. When the FBX file is modified inside the originating 3D modeling software, Unity updates the Model Prefab.

You should checkout this package and its features though which are specifically made to round trip FBX files between Unity and Maya / 3ds Max,

Solution 2:[2]

Check out the AssetDatabase function RenameAsset.

DOCS: "Rename an asset file."

var path = AssetDatabase.GetAssetPath(fbx);
var newName = "new name";

AssetDatabase.RenameAsset(path, newName);

Reference for RenameAsset

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 hijinxbassist