Hello,
I created a prefab from my hierarchy with several meshes and materials.
Then I updated my prefab, adding new objects inside (so adding new meshes) and also removing some objects. But the meshes and the materials are still in the prefab.
When unfolding the prefab in the project window, I can see the unused meshes (I’m sure they are not used as the find references in scene returns nothing).
How can I edit this prefab to remove the unused meshes (same question with materials) ?
If I open the prefab, I can only change its structure.
To remove a mesh or material from a prefab, you can either do it manually by opening the prefab in the prefab editor and deleting the unused mesh or material, or you can use a script to do it automatically.
Here’s how you can remove a mesh or material from a prefab using a script:
First, create a new script and attach it to the prefab that you want to edit.
In the script, add a function that will be called when you want to remove the unused mesh or material from the prefab. This function should take the prefab as an argument and should return the updated prefab with the unused mesh or material removed.
Inside the function, use the GetComponentsInChildren function to get a list of all the meshes and materials that are currently part of the prefab.
Iterate through the list of meshes and materials and check if each one is still being used by the prefab. You can do this by using the GetComponent function to check if the prefab has any components that use the mesh or material.
If a mesh or material is not being used by the prefab, use the DestroyImmediate function to delete it from the prefab.
After you have deleted all the unused meshes and materials, use the PrefabUtility.SaveAsPrefabAsset function to save the updated prefab.
I can’t find how to remove it manually. When I open the prefab, I can update its hierarchy, then can remove the game object using the mesh, or the mesh filter component. But the mesh objects still is in the prefab file (I can see it if I unfold the prefab in the project window). For example, I want to remove the Mesh_37173 that is in my prefab, but not used in the prefab structure.
So I tried by script, but the GetComponentsInChildren function returns an error if I try to get a Mesh. I can only get the Meshfilter, then I can only access to the ones used.
(ArgumentException: GetComponent requires that the requested component ‘Mesh’ derives from MonoBehaviour or Component or is an interface.).