- Home /
Mesh and material disappear after CreatePrefab() in C#
I'm currently working on a model importing tool in C# for the Unity Editor, which runs OnPostProcessAllAssets(), reads each model file and is supposed to create a prefab for each one containing the Mesh that was constructed from the model file data.
I have gone about doing this by creating an empty GameObject for each model, and then I assign the Mesh to the GameObject's MeshFilter.mesh. It's clear to me that this works, since I can see the applied Mesh in the Scene view.
However, the problem arises when this GameObject is converted into a prefab:
The prefab does not contain a Mesh! It has a MeshFilter component, but the Mesh comes up as "None (Mesh)". Furthermore, the material/shader that I assign to the MeshRenderer also disappears.
The creation of the GameObject in the Scene goes like this:
Mesh importedMesh = null;
importedMesh = ImportModelFromXml(asset);
string prefabName = fileName.Remove( fileName.LastIndexOf(MODEL_SUFFIX) );
GameObject go = new GameObject(prefabName);
go.AddComponent<MeshRenderer>().material = new Material( Shader.Find("Transparent/Diffuse") );
go.AddComponent<MeshFilter>().mesh = importedMesh;
And ImportMexhFromXml(string asset) looks like this (it's not reading from a file yet -- just creating a unit tetrahedron as a test):
static Mesh ImportModelFromXml(string filePath)
{
Debug.Log("Reading .XML model: " + filePath);
#region Read through the .XML format and assign all of the mesh data to a single mesh
Vector3 p0 = new Vector3(0f, 0f, 0f);
Vector3 p1 = new Vector3(1f, 0f, 0f);
Vector3 p2 = new Vector3(0.5f, 0, Mathf.Sqrt(0.75f) );
Vector3 p3 = new Vector3(0.5f, Mathf.Sqrt(0.75f), Mathf.Sqrt(0.75f) / 3) ;
Mesh importedMesh = new Mesh();
importedMesh.vertices = new Vector3[] {
p0, p1, p2,
p0, p2, p3,
p2, p1, p3,
p0, p3, p1
};
importedMesh.triangles = new int[] {
0, 1, 2,
3, 4, 5,
6, 7, 8,
9, 10, 11
};
importedMesh.RecalculateNormals();
importedMesh.RecalculateBounds();
importedMesh.Optimize();
#endregion
Debug.Log(".XML Mesh import complete!");
// Return the Mesh to the caller
return importedMesh;
}
I am using the following statement to create the prefab from the GameObject in the Scene:
PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ReplaceNameBased);
Could someone possibly explain why my Mesh is disappearing when I make this GameObject into a prefab? Do I have to do something special to bring the component contents over to the prefab?
Answer by Paulius-Liekis · Dec 04, 2012 at 11:30 AM
I think this is what's happening: your material and mesh are created as plain C# objects. Once you assign them to the GameObject they are linked and saved to Scene file. When you create a Prefab it copies your object to Project, but it does not do the same for your Mesh and Material, i.e. a project-level object can not reference scene-level objects. You have to understand this boundary between project and scene objects.
The solution: store Mesh and Material in your prefab. I don't remember function exactly, but there is something like add-asset-to-prefab or add-object-to-asset.
I understand your concern here, which is why I was suspicious that maybe something else needed to be done to map the mesh to the prefab. However, seeing as I'm able to see the GameObject with the material and mesh applied in the scene it was my understanding that they were scene objects. Unity seems to be treating them as such.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Create destroyed prefab 1 Answer