- Home /
Creating a mesh and assigning a material during edit time.
Hello all, I am working on some code that will generate layouts from xml files. I am currently trying to make plane in code and assign it a material based on the imported xml file. However I have run into a few issues. When I create the plane on its own with no material every thing works fine. I get a plane with the ugly purple color, but when I set the material on the plane the mesh disappears in the inspector.
var loader = (frameObjectInfo.info as QuickBackdropLoader);
int imageHandle = loader.ImageHandle;
go = synPlaneUtility.CreatePlane(1, 1, loader.Width, loader.Height);
//Material mat = new Material(Shader.Find("Diffuse"));
if(go.renderer.sharedMaterial == null)
{
go.renderer.sharedMaterial = new Material(Shader.Find("Diffuse"));
}
go.renderer.sharedMaterial.mainTexture = textureLibrary[imageHandle];
I have tried setting both material and shared material, and setting either one causes the mesh to disappear. Any help would be greatly appreciated
The mesh generation works fine when I do not assign a material to the mesh. As soon as I try to assign a material to the mesh it disappears. The code for generating the plane is a modified version of the script found here: http://wiki.unity3d.com/index.php/CreatePlane I simply made it so that the function can be called with parameters ins$$anonymous$$d of having to use the wizard.
$$anonymous$$y guess is that you have the normals reversed. That is, you plane is facing down. Try moving your camera in the inspector to view the bottom side (or rotate the plane 180 degrees).
Answer by Aeal · Mar 19, 2013 at 02:54 AM
Ok, so after some testing and hair pulling I finally came up with the solution. The problem was not in my mesh generation or material assigning. The problem was the way the code worked as a whole. The importer works in several steps. First it imports all the sprite textures, then it makes prefabs for each object in the layout and then generates the layout itself. When generating the prefab the mesh and material would be dynamically generated. However you cannot have a reference to a dynamically created mesh and material in a prefab (thus why they would disappear when created in layout).
So my solution to the problem was to add the mesh and material to the prefab using the AssetDatabase AddObjectToAsset function, and then load the newly created objects and assign them to the appropriate values in the prefab.
Your answer
Follow this Question
Related Questions
Mesh visible in editor but not build 1 Answer
Adding Verticies to Mesh 1 Answer
Why does Frame Selected toggle between position and bounds for procedural and imported meshes? 1 Answer
Multiple materials vs Multiple Meshes. Which is better for performance? 0 Answers
How to set up UV for lightmaps for generated meshes? 4 Answers