Hello world!
Frans van Berendonk / News / Creating meshes at runtime in Unity

Creating meshes at runtime in Unity

There comes a point where you'll want to create your own meshes at runtime dynamicly. Diving into code that generates 3D objects is always a bit daunting due to the required math, but I was pleasantly surprised Unity has a pretty decent example sitting in the docs. The main thing to remember is that in order to actually render a mesh, you'll need a few other components such as a MeshRenderer and MeshFilter. That sounds rather obvious, but when creating entire objects from code it's something you might omit out of habit - as adding something to the Hierarchy adds these components by default!

In retrospect it wasn't all that hard to wrap the example code into a component of it's own with a DrawQuad method, that takes a width and height as parameters. I'm curious to see what features will call for an implementation like this, but I've always made a point of being able to generate the simplest objects of a framework strictly from code. Understanding the basics opens up a lot of doors after all. I can almost smell the procedurally generated dungeons :)