Danickar
Danickar

Reputation: 85

How to add text to generated mesh in Unity

enter image description here

I have created this icosahedron in code so that each triangle is its own mesh instead of one solid mesh with 20 faces. I want to add text on the outward side of each triangle that is roughly centered. For a simple visual think a 20 sided die for DnD.

The constraints would be that while this version is a simple icosahedron with only the 20 faces, the script is written so that it can be refined recursively to add more faces and turn it into an icosphere. I still want the text to scale and center as the number of triangles grows.

I currently have a prefab "triangle" object that holds the script each triangle will need and as well as a mesh filter/mesh renderer.

The only other thing in the scene is an empty GameObject with the script attached that creates the triangles and acts as the parent for all of them.

I have attempted various ways of adding text into the triangle prefab. The problem I keep running into is that since the mesh itself does not exist in the prefab I am not sure how to orient or scale the text so that it would appear in the correct place.

I am not sure if the text needs to be completely generated at run time with the triangles, or if there is a way to add it to the prefab and then update it so that it scales and positions as the triangle is created.

My Google searching so far has only really brought me results that involve objects permanently in the scene, nothing on what to do with meshes generated at run time.

Upvotes: 1

Views: 402

Answers (1)

Ramon de Llano
Ramon de Llano

Reputation: 375

Is the text a texture saved in png or other image format? Because if it is the case, you can just create a material with the texture that holds the text. The next step is to define to each vertex its UV, wraping this way the texture on the triangle. If the text has to be generated on the fly, the best approach I can think right now is to add a Text Mesh to the triangle (since it is a prefab) and access to the component.

Upvotes: 1

Related Questions