1

I am trying to change and resize textures put on faces of bilt-in cube in Unity. Without any problems I managed to put different image on every face with this code:

void Start()
{
    Mesh mesh = GetComponent<MeshFilter>().mesh;
    Vector2[] UVs = new Vector2[mesh.vertices.Length];
    // Front
    UVs[0] = new Vector2(0.0f, 0.0f);
    UVs[1] = new Vector2(0.333f, 0.0f);
    UVs[2] = new Vector2(0.0f, 0.333f);
    UVs[3] = new Vector2(0.333f, 0.333f);
    // Top
    UVs[4] = new Vector2(0.334f, 0.333f);
    UVs[5] = new Vector2(0.666f, 0.333f);
    UVs[8] = new Vector2(0.334f, 0.0f);
    UVs[9] = new Vector2(0.666f, 0.0f);
    // Back
    UVs[6] = new Vector2(1.0f, 0.0f);
    UVs[7] = new Vector2(0.667f, 0.0f);
    UVs[10] = new Vector2(1.0f, 0.333f);
    UVs[11] = new Vector2(0.667f, 0.333f);
    // Bottom
    UVs[12] = new Vector2(0.0f, 0.334f);
    UVs[13] = new Vector2(0.0f, 0.666f);
    UVs[14] = new Vector2(0.333f, 0.666f);
    UVs[15] = new Vector2(0.333f, 0.334f);
    // Left
    UVs[16] = new Vector2(0.334f, 0.334f);
    UVs[17] = new Vector2(0.334f, 0.666f);
    UVs[18] = new Vector2(0.666f, 0.666f);
    UVs[19] = new Vector2(0.666f, 0.334f);
    // Right        
    UVs[20] = new Vector2(0.667f, 0.334f);
    UVs[21] = new Vector2(0.667f, 0.666f);
    UVs[22] = new Vector2(1.0f, 0.666f);
    UVs[23] = new Vector2(1.0f, 0.334f);
    mesh.uv = UVs;
    GetComponent<MeshFilter>().mesh = mesh
     }

This works, but it uses only texture (texture atlas??) attached to the cube/shader. I wonder if it is possible to use two different texture sets so for example I have 4 faces with the parts of the texture 1, and 2 faces with parts of texture 2. Or maybe it is possible to generate texture atlas by code using 6 different textures? Can PackTexture() method be used for that?

Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
Łukasz Motyczka
  • 1,169
  • 2
  • 13
  • 35

1 Answers1

1

To use more textures you need a separate submesh. With separate subMesh Unity allow to attach other material and other texture.

Look at

http://docs.unity3d.com/ScriptReference/Mesh.SetTriangles.html

For example create a script with this code and attach it to a GameObject with a cube.

void Awake () 
{
    Mesh currentMesh = this.GetComponent<MeshFilter>().mesh;

    int[] submesh0 = new int[]{0,2,3,0,3,1,8,4,5,8,5,9,10,6,7,10,7,11,12,13,14,12,14,15};
    int[] submesh1 = new int[]{16,17,18,16,18,19, 20,21,22,20,22,23};

    currentMesh.subMeshCount=2;

    currentMesh.SetTriangles(submesh0,0);
    currentMesh.SetTriangles(submesh1,1);
}

After the awake, attach two material to cube and it uses for 4 face a material, for other 2 the other.

Please note that this is not the best way to this. Using two material break unity optimization and need two separate draw calls (more expensive).

Klamore74
  • 578
  • 1
  • 6
  • 21
  • Hi. Thanks, but if I will have two submeshes attached to cube, one having six cube faces texture and secon also having different six faces texture, will I be able to use four faces from first texture and two faces from second texture? – Łukasz Motyczka Mar 25 '16 at 16:04
  • Hi, I solved my problem with using Texture2D.PackTextures, and then by setting UVs for every opf packed textures. Thanks anyway :) – Łukasz Motyczka Mar 26 '16 at 17:59