Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class MineCraftCharacter : MonoBehaviour
  7. {
  8.  
  9. private void Awake()
  10. {
  11. int vertexCount = 24;
  12. int triangleCount = 36;
  13. int uvCount = 24;
  14. int cubeCount = 6;
  15.  
  16. Vector3[] vertices = new Vector3[vertexCount* cubeCount];
  17. int[] triangles = new int[triangleCount * cubeCount];
  18. Vector2[] uv = new Vector2[uvCount * cubeCount];
  19.  
  20. MeshFilter mf = GetComponent<MeshFilter>();
  21. Mesh m = new Mesh();
  22.  
  23. for (int i = 0; i < vertexCount; i++)//버텍스 생성
  24. {
  25. vertices[i] = CreateMeshCube(new Vector3(-5, -5, -5), 5f, 5f, 5f)[i];//머리
  26. vertices[i + vertexCount] = CreateMeshCube(new Vector3(-10f, -20f, -5), 2.5f, 2.5f, 7.5f)[i];//왼팔
  27. vertices[i + vertexCount * 2] = CreateMeshCube(new Vector3(5f, -20f, -5), 2.5f, 2.5f, 7.5f)[i];//오른팔
  28. vertices[i + vertexCount * 3] = CreateMeshCube(new Vector3(-5f, -20f, -5), 2.5f, 5f, 7.5f)[i];//몸통
  29. vertices[i + vertexCount * 4] = CreateMeshCube(new Vector3(-5f, -35f, -5), 2.5f, 2.5f, 7.5f)[i];//오른다리
  30. vertices[i + vertexCount * 5] = CreateMeshCube(new Vector3(0f, -35f, -5), 2.5f, 2.5f, 7.5f)[i];//왼다리
  31.  
  32. }
  33.  
  34. for (int i = 0; i < triangleCount; i++)//두르기
  35. {
  36. for (int j = 0; j < cubeCount; j++)
  37. triangles[i + triangleCount * j] = GetTriangles(j + 1)[i];
  38. }
  39.  
  40. for (int i = 0; i < uvCount; i++)//uv그리기
  41. {
  42. uv[i] = DrawUVCube(new Vector2(0f, 6f / 8f), 1f, 1f, 1f)[i];//머리
  43. uv[i + uvCount] = DrawUVCube(new Vector2(5f / 8f, 4f / 8f), 0.5f, 0.5f, 1.5f)[i];//왼팔
  44. uv[i + uvCount * 2] = DrawUVCube(new Vector2(4f / 8f, 0f), 0.5f, 0.5f, 1.5f)[i];//오른팔
  45. uv[i + uvCount * 3] = DrawUVCube(new Vector2(2f / 8f, 4f / 8f), 0.5f, 1f, 1.5f)[i];//몸통
  46. uv[i + uvCount * 4] = DrawUVCube(new Vector2(0f , 4f / 8f), 0.5f, 0.5f, 1.5f)[i];//오른다리
  47. uv[i + uvCount * 5] = DrawUVCube(new Vector2(2f / 8f, 0), 0.5f, 0.5f, 1.5f)[i];//왼다리
  48.  
  49. }
  50.  
  51. m.vertices = vertices;
  52. m.triangles = triangles;
  53. m.uv = uv;
  54.  
  55. mf.mesh = m;
  56.  
  57. }
  58.  
  59. //버텍스 생성
  60. Vector3[] CreateMeshCube(Vector3 startPos, float horiz, float vertical, float height)//시작점, 크기
  61. {
  62. Vector3[] reVal;
  63. reVal = new Vector3[] {
  64.  
  65. new Vector3(startPos.x,startPos.y,startPos.z+horiz*2f),
  66. new Vector3(startPos.x,startPos.y+height*2f,startPos.z+horiz*2f),
  67. new Vector3(startPos.x,startPos.y+height*2f,startPos.z),
  68. new Vector3(startPos.x,startPos.y,startPos.z),//오른쪽
  69.  
  70. new Vector3(startPos.x,startPos.y,startPos.z),
  71. new Vector3(startPos.x,startPos.y+height*2f, startPos.z),
  72. new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z),
  73. new Vector3(startPos.x+vertical*2f,startPos.y, startPos.z),//앞
  74.  
  75. new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z),
  76. new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z),
  77. new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z+horiz*2f),
  78. new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z+horiz*2f),//왼
  79.  
  80. new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z+horiz*2f),
  81. new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z+horiz*2f),
  82. new Vector3(startPos.x,startPos.y+height*2f,startPos.z+horiz*2f),
  83. new Vector3(startPos.x,startPos.y,startPos.z+horiz*2f),//뒤
  84.  
  85. new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z+horiz*2f),
  86. new Vector3(startPos.x+vertical*2f,startPos.y+height*2f,startPos.z),
  87. new Vector3(startPos.x,startPos.y+height*2f,startPos.z),
  88. new Vector3(startPos.x,startPos.y+height*2f,startPos.z+horiz*2f),//위
  89.  
  90. new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z),
  91. new Vector3(startPos.x+vertical*2f,startPos.y,startPos.z+horiz*2f),
  92. new Vector3(startPos.x,startPos.y,startPos.z+horiz*2f),
  93. new Vector3(startPos.x,startPos.y,startPos.z),//밑
  94.  
  95. };
  96.  
  97.  
  98. return reVal;
  99. }
  100.  
  101. //두르기 순서
  102. int[] GetTriangles(int count)
  103. {
  104. int[] reVal = new int[36];
  105. count -= 1;
  106. count *= 24;
  107.  
  108. reVal = new int[]
  109. {
  110. count+0,count+1,count+2,
  111. count+0,count+2,count+3,//오
  112.  
  113. count+4,count+5,count+6,
  114. count+4,count+6,count+7,//앞
  115.  
  116. count+8,count+9,count+10,
  117. count+8,count+10,count+11,//왼
  118.  
  119. count+12,count+13,count+14,
  120. count+12,count+14,count+15,//뒤
  121.  
  122. count+16,count+17,count+18,
  123. count+16,count+18,count+19,//위
  124.  
  125. count+20,count+21,count+22,
  126. count+20,count+22,count+23//아래
  127. };
  128. return reVal;
  129. }
  130.  
  131. //uv씌우기
  132. Vector2[] DrawUVCube(Vector2 startPos, float horiz, float vertical, float height)//시작점, 크기
  133. {
  134. Vector2[] reVal = new Vector2[]
  135. {
  136. new Vector2((0f * vertical/8f) + 0f * (horiz/8f) + startPos.x,startPos.y),
  137. new Vector2((0f * vertical/8f) + 0f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  138. new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  139. new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x, startPos.y),//오
  140.  
  141. new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y),
  142. new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  143. new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  144. new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x, startPos.y),//앞
  145.  
  146. new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y),
  147. new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  148. new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  149. new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x, startPos.y),//뒤
  150.  
  151. new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y),
  152. new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  153. new Vector2((2f * vertical/8f) + 2f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  154. new Vector2((2f * vertical/8f) + 2f * (horiz/8f) + startPos.x, startPos.y),//왼
  155.  
  156.  
  157. new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  158. new Vector2((0f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
  159. new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
  160. new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),//위
  161.  
  162. new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),
  163. new Vector2((1f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
  164. new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f + horiz/8f),
  165. new Vector2((2f * vertical/8f) + 1f * (horiz/8f) + startPos.x,startPos.y + height/8f),//아래
  166. };
  167. return reVal;
  168. }
  169.  
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement