You should build the cube in an external editor and assign an independent material per face. Then, when you import it into unity, you can index the material slots and change the color at will. Good luck :)

A couple more ideas. If you build each face with its own vertices, the you can change the uv coordinates for each face depending on what color you want it to be. I've not played with vertex color, but I believe you could reset the vertex colors for a face as well. RaycastHit gives back a triangle index, so you can build a map between each triangle and its vertices so when you get a click on a triangle you know which uv coordinates to change or which vertices to assign new colors to.

I don't understand ur ideas... A new cube...Then i have the Problem that i have 16*16 cubes in 256 Chunks...that are 16*16*256 cubes. That give a huge lag is think. Colors32 doesnt work too. And how can i build each face with own vertices? And how to detect it

1 Reply

The basic idea is to build each face with its own vertices. This increases the vertex count, but not the triangle count. Each face has it own set of vertices, then you can color the face by either by using a Vertex Colored shader and changing the vertex colors, or by using a texture atlas and changing the UV coordinates to a different point in the atlas.

There are three scripts below. The first is a very simple vertex shader, the second creates a cube mesh and processes the hit on a triangle in that mesh. The third processes the mouse click and passed the triangle index. If you setup all three correctly, you will be able to click on a cube face to change it to random color.

This is the simplest vertex color shader I know. If you Google "Unity3d Vertex Colored Shader" you will find others with more features.

The following code builds a cube. The mesh does not have to be a cube to work. The more important ingredient is that each face you want to color has its own set of vertices. Attach the script to an empty game object that has these three components: a) MeshCollider b) Renderer, c) MeshFilter.

Note the statement "int iStart = mesh.triangles[iTriangle*3];" works to find the beginning index of the four vertices (or four colors matched to the indices) because the two trangles both use the first of the four points as the start of each rectangle.

And here is a script that does the Raycasting and triggers the change in color. Attach it to an empty game object.

You are trying to be super efficient by only generating the sides you need. And this is going to cause you a bunch of programming pain to make it work. It can be done, but you will need to track sides and have use counts for sides, and a bunch of other stuff. Before you go to all that work, I'd make sure it is necessary.

Take the code that makes a cube that I posted above and make it into a prefab. Spawn 256 of them in some pattern representative of your game. Put it on the target device and see how it performs. All of the cubes should be batched, so there will only be one draw call. Plus given what you are building, a high percentage of the faces will be culled. I suspect that having 256 game object will work just fine on most mobile platforms.

If you have a single game object per cube, then processes like deleting and saving colors becomes easy to program.