Unity - 如何通过脚本重命名网格?
Unity - How to rename Mesh via script?
我正在编写直接在 Unity 中重命名 Mesh 的脚本,因为每次重命名 FBX 文件时,网格资产仍然保留上次从 3dsMax 的 Maya 导出时的旧名称,这真的很烦人.我尝试过的都不起作用,代码如下:
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);
}
}
我也试过:
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;
}
第三种方法可以在FBX的Meshfilter组件上更改Mesh的名称,但实际子资源仍然使用旧名称,您可以通过附图找到我的意思。
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);
}
我该如何解决这个问题?
如果我从 DCC 中重新导出 FBX 以更改 Mesh 的名称,它会丢失 link 与 prefab 但 fbx 文件的 guid 实际上并没有改变,为什么会这样?我的步骤是
- 将 aa.FBX 文件统一重命名为 bb.FBX
- 将bb.FBX导入3dsmax,并在3dsmax中将名称由aa改为bb
- 再次导出为统一 bb.FBX。
查看 AssetDatabase 函数 RenameAsset。
DOCS: "Rename an asset file."
var path = AssetDatabase.GetAssetPath(fbx);
var newName = "new name";
AssetDatabase.RenameAsset(path, newName);
我认为这不能直接在 Unity 中完成。
原因:你在Unity中看到的assets是一个已经导入的虚拟模型Prefab。实际上,在您的系统文件浏览器中,它仍然是一个普通的 FBX 文件,Unity 不会直接将内容写入该 FBX 文件。 Unity 不是 3D 建模软件,因此没有尝试解决这个问题。
也就是说从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.
您应该查看此包及其功能,这些功能专门用于在 Unity 和 Maya/3ds Max 之间往返 FBX 文件,
我正在编写直接在 Unity 中重命名 Mesh 的脚本,因为每次重命名 FBX 文件时,网格资产仍然保留上次从 3dsMax 的 Maya 导出时的旧名称,这真的很烦人.我尝试过的都不起作用,代码如下:
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);
}
}
我也试过:
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;
}
第三种方法可以在FBX的Meshfilter组件上更改Mesh的名称,但实际子资源仍然使用旧名称,您可以通过附图找到我的意思。
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);
}
我该如何解决这个问题?
如果我从 DCC 中重新导出 FBX 以更改 Mesh 的名称,它会丢失 link 与 prefab 但 fbx 文件的 guid 实际上并没有改变,为什么会这样?我的步骤是
- 将 aa.FBX 文件统一重命名为 bb.FBX
- 将bb.FBX导入3dsmax,并在3dsmax中将名称由aa改为bb
- 再次导出为统一 bb.FBX。
查看 AssetDatabase 函数 RenameAsset。
DOCS: "Rename an asset file."
var path = AssetDatabase.GetAssetPath(fbx);
var newName = "new name";
AssetDatabase.RenameAsset(path, newName);
我认为这不能直接在 Unity 中完成。
原因:你在Unity中看到的assets是一个已经导入的虚拟模型Prefab。实际上,在您的系统文件浏览器中,它仍然是一个普通的 FBX 文件,Unity 不会直接将内容写入该 FBX 文件。 Unity 不是 3D 建模软件,因此没有尝试解决这个问题。
也就是说从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.
您应该查看此包及其功能,这些功能专门用于在 Unity 和 Maya/3ds Max 之间往返 FBX 文件,