Drawing a series of triangles using scripts

I am new to Unity and attempting to translate one of my games to Unity. A major component of the game is drawing a tree of triangles, which wasn’t that difficult in the original language (Processing), but I am having a lot of trouble understanding how to do this in Unity. The original algorithm works like so:

  • Create a Layer object (stores transforms)
  • Inside the layer object, create a Tree object (stores array of branches)
  • Inside the Tree object, create an array of Branch objects (triangles) using the previous branch’s vertices to determine where the new one will be drawn

So far, the best equivalent I’ve been able to come up with is using prefabs in place of the Layer, Tree, and Branch classes (making the branches children of Tree, and Tree a child of Layer), and creating a mesh for each branch by manipulating the MeshFilter component for each Branch prefab. Everything is instantiated using scripts. I am a bit worried that this mesh method may be too expensive, and also not be able to render things like stroke as I was able to when in a 2D system. Even if this is the correct approach, I’m not 100% sure how to implement it in Unity.

What is the correct way to build something like this? I feel a bit lost coming from an OOP language to Unity’s stage-like environment.

You can still have the classes you want in unity. The scripts that show up in the inspector are just derived from the MonoBehaviour class. You can define your own classes like normal and then have a MonoBehaviour manipulate them.

Using Instantiate can be very expensive, So it would probably be best to have a Pool of branch meshes that just recycled and regenerated when it passes the camera.

Meshes can be built by using an array of Vector3s for each vertex. Triangles are made using an array of ints, each value is used to index the vertex array. The triangle array must be exactly three times longer than the vertex array.