Hi,
I have generated 3D mesh in Unity and now I am trying to add UVs to this mesh. I managed to do that when it was in 2D. But I don’t know how to do it in 3D. Do you guys have any idea? Thank you for your help.
EDIT: I have added code for my hex mesh
using UnityEngine;
using System.Collections;
public class HexInfo : MonoBehaviour {
public Texture texture;
// Basix hexagon mesh making
private Vector3[] vertices;
private Vector2[] uv;
private int[] triangles;
void Start ()
{
MeshSetup();
}
void MeshSetup()
{
#region VERTICES
float height = 0.125f;
float floorLevel = 0.0f + height;
vertices = new Vector3[]
{
// Top
new Vector3(-1.0f, floorLevel, -0.5f),
new Vector3(-1.0f, floorLevel, 0.5f),
new Vector3(0.0f, floorLevel, 1.0f),
new Vector3(1.0f, floorLevel, 0.5f),
new Vector3(1.0f, floorLevel, -0.5f),
new Vector3(0.0f, floorLevel, -1.0f),
// Bottom
new Vector3(-1.0f, floorLevel - height, -0.5f),
new Vector3(-1.0f, floorLevel - height, 0.5f),
new Vector3(0.0f, floorLevel - height, 1.0f),
new Vector3(1.0f, floorLevel - height, 0.5f),
new Vector3(1.0f, floorLevel - height, -0.5f),
new Vector3(0.0f, floorLevel - height, -1.0f)
};
#endregion
#region TRIANGLES
triangles = new int[]
{
// Top
1, 5, 0,
1, 4, 5,
1, 2, 4,
2, 3, 4,
// Bottom
7, 6, 11,
7, 11, 10,
7, 10, 8,
8, 10, 9,
// Sides
0, 11, 6,
0, 5, 11,
5, 10, 11,
5, 4, 10,
4, 9, 10,
4, 3, 9,
3, 8, 9,
3, 2, 8,
2, 7, 8,
2, 1, 7,
1, 6, 7,
1, 0, 6
};
#endregion
#region UV
uv = new Vector2[]
{
/*
new Vector2(0.0f, 0.25f),
new Vector2(0.0f, 0.75f),
new Vector2(0.5f, 1.0f),
new Vector2(1.0f, 0.75f),
new Vector2(1.0f, 0.25f),
new Vector2(0.5f, 0.0f),
*/
};
#endregion
#region FINALIZE
// Add a mesh filter to the game object the script is attached to. Cache it for later
MeshFilter meshFilter = this.gameObject.AddComponent<MeshFilter>();
// Add a mesh renderer to the game object to the script is attached to
this.gameObject.AddComponent<MeshRenderer>();
// Create a mesh object to pass our data into
Mesh mesh = new Mesh();
// Add our vertices to the mesh
mesh.vertices = vertices;
// Add our triangles to the mesh
mesh.triangles = triangles;
// Add our uv coordinates to the mesh
//mesh.uv = uv;
// Recalculate normals for lighting
mesh.RecalculateNormals();
// Set the gameobject's meshFilter's mesh to tbe the one we just made
meshFilter.mesh = mesh;
// Test our uv
this.renderer.material.mainTexture = texture;
#endregion
}
}
