So I’ve just constructed a Cylinder by code (I know its sloppy) but when I apply a texture and compare it to a normal unity cylinder the texture seems backwards I think. If anyone knows what I did wrong and can help me fix this I’d appreciate it A LOT. Also if you could educate me on how a for loop is made for the vertices and triangles that would be cool too.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class BuildMesh : MonoBehaviour
{
//Litterally just for me to remember where locations are (They have no purpose)
public Vector3 verticesLoc;
public Vector3 verticesLocBack;
// Use this for initialization
void Start()
{
MeshFilter mf = GetComponent<MeshFilter>();
Mesh mesh = mf.mesh;
//Vertices
Vector3[] vertices = new Vector3[]
{
//Constructed Clockwise (So is visible from outside)
#region Front Face
//Top
new Vector3(0.0f, 0.5f, 1), //0
new Vector3(-0.15f, 0.475f, 1), //1
new Vector3(-0.295f, 0.405f, 1), //2
new Vector3(-0.405f, 0.295f, 1), //3
new Vector3(-0.475f, 0.15f, 1), //4
//Right
new Vector3(-0.5f, 0.0f, 1), //5
new Vector3(-0.475f, -0.15f, 1), //6
new Vector3(-0.405f, -0.295f, 1), //7
new Vector3(-0.295f, -0.405f, 1), //8
new Vector3(-0.15f, -0.475f, 1), //9
//Bottom
new Vector3(0.0f, -0.5f, 1), //10
new Vector3(0.15f, -0.475f, 1), //11
new Vector3(0.295f, -0.405f, 1), //12
new Vector3(0.405f, -0.295f, 1), //13
new Vector3(0.475f, -0.15f, 1), //14
//Left
new Vector3(0.5f, 0f, 1), //15
new Vector3(0.475f, 0.15f, 1), //16
new Vector3(0.405f, 0.295f, 1), //17
new Vector3(0.295f, 0.405f, 1), //18
new Vector3(0.15f, 0.475f, 1), //19
//Center
new Vector3(0.0f, 0.0f, 1), //20
#endregion
//Constructed Counter Clockwise (So is visible from outside)
#region Back Face
//Top
new Vector3(0.0f, 0.5f, -1), //21
new Vector3(0.15f, 0.475f, -1), //22
new Vector3(0.295f, 0.405f, -1), //23
new Vector3(0.405f, 0.295f, -1), //24
new Vector3(0.475f, 0.15f, -1), //25
//Left
new Vector3(0.5f, 0f, -1), //26
new Vector3(0.475f, -0.15f, -1), //27
new Vector3(0.405f, -0.295f, -1), //28
new Vector3(0.295f, -0.405f, -1), //29
new Vector3(0.15f, -0.475f, -1), //30
//Bottom
new Vector3(0.0f, -0.5f, -1), //31
new Vector3(-0.15f, -0.475f, -1), //32
new Vector3(-0.295f, -0.405f, -1), //33
new Vector3(-0.405f, -0.295f, -1), //34
new Vector3(-0.475f, -0.15f, -1), //35
//Right
new Vector3(-0.5f, 0.0f, -1), //36
new Vector3(-0.475f, 0.15f, -1), //37
new Vector3(-0.405f, 0.295f, -1), //38
new Vector3(-0.295f, 0.405f, -1), //39
new Vector3(-0.15f, 0.475f, -1), //40
//Center
new Vector3(0.0f, 0.0f, -1) //41
#endregion
};
//Triangles 3 points clockwise determines side visable
int[] triangle = new int[]
{
//Octogon numbers are related to comments next to vertices
#region Front Face
0,1,20,
1,2,20,
2,3,20,
3,4,20,
4,5,20,
5,6,20,
6,7,20,
7,8,20,
8,9,20,
9,10,20,
10,11,20,
11,12,20,
12,13,20,
13,14,20,
14,15,20,
15,16,20,
16,17,20,
17,18,20,
18,19,20,
19,0,20,
#endregion
#region Back Face
21,22,41,
22,23,41,
23,24,41,
24,25,41,
25,26,41,
26,27,41,
27,28,41,
28,29,41,
29,30,41,
30,31,41,
31,32,41,
32,33,41,
33,34,41,
34,35,41,
35,36,41,
36,37,41,
37,38,41,
38,39,41,
39,40,41,
40,21,41,
#endregion
//Goes clock wise around the cylinder starting at the top (Right top, Right Bottom, Left Bottom, Left Top)
#region Body
//Top Right Section
0,21,40,
40,1,0,
1,40,39,
39,2,1,
2,39,38,
38,3,2,
3,38,37,
37,4,3,
4,37,36,
36,5,4,
//Bottom Right Section
5,36,35,
35,6,5,
6,35,34,
34,7,6,
7,34,33,
33,8,7,
8,33,32,
32,9,8,
9,32,31,
31,10,9,
//Bottom Left Section
10,31,30,
30,11,10,
11,30,29,
29,12,11,
12,29,28,
28,13,12,
13,28,27,
27,14,13,
14,27,26,
26,15,14,
//Top Left Section
15,26,25,
25,16,15,
16,25,24,
24,17,16,
17,24,23,
23,18,17,
18,23,22,
22,19,18,
19,22,21,
21,0,19
#endregion
};
//For loop to take x and y of vertices to construct UVS
Vector2[] UVS = new Vector2[vertices.Length];
for (var i = 0; i < vertices.Length; i++)
{
UVS <em>= new Vector2(vertices_.x, vertices*.y);*_</em>
}
//Clear mesh values
mesh.Clear();
//Put in my values
mesh.vertices = vertices;
mesh.triangles = triangle;
mesh.uv = UVS;
//Put back togeather
MeshUtility.Optimize(mesh);
mesh.RecalculateNormals();
//Create a mesh collider on this object and assign it to the mesh
MeshCollider meshCollider = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
meshCollider.sharedMesh = mesh;
}
}