Advertisement
Gman0064

Tile Map paste

Jun 30th, 2017
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(MeshFilter))]
  6. [RequireComponent(typeof(MeshCollider))]
  7. [RequireComponent(typeof(MeshRenderer))]
  8. public class TileMap : MonoBehaviour {
  9.  
  10. public int size_x = 100;
  11. public int size_z = 50;
  12. public float tileSize = 1.0f;
  13.  
  14. public Texture2D terrainTiles;
  15. Texture2D texture;
  16. public int tileResolution = 16;
  17.  
  18. Vector3 currentTileCoord;
  19. public Transform selectionCube;
  20.  
  21. Color[][] tiles;
  22. Color[] p;
  23. Color[] q;
  24.  
  25. int numTilesPerRow;
  26. int numRows;
  27. public float scale;
  28.  
  29. void Start()
  30. {
  31. BuildMesh();
  32. }
  33.  
  34. //Chops up the sprite sheet into tiles, to be used by BuildTexture()
  35. Color[][] ChopUpTiles()
  36. {
  37. numTilesPerRow = terrainTiles.width / tileResolution;
  38. numRows = terrainTiles.height / tileResolution;
  39.  
  40. Color[][] tiles = new Color[numTilesPerRow * numRows][];
  41.  
  42. for(int y=0; y<numRows; y++)
  43. {
  44. for (int x = 0; x<numTilesPerRow; x ++)
  45. {
  46. tiles[y*numTilesPerRow + x] = terrainTiles.GetPixels(x*tileResolution, y*tileResolution, tileResolution, tileResolution);
  47. }
  48. }
  49.  
  50. return tiles;
  51. }
  52.  
  53. //Builds the texture for the map, tile by tile
  54. void BuildTexture()
  55. {
  56. int texWidth = size_x * tileResolution;
  57. int texHeight = size_z * tileResolution;
  58. texture = new Texture2D(texWidth, texHeight);
  59.  
  60. tiles = ChopUpTiles();
  61.  
  62. for (int y=0; y <size_z; y++)
  63. {
  64. for(int x=0; x<size_x; x++)
  65. {
  66. //Perlin Noise Code
  67. //This sets the color of the tile to the number corresponding to the Spritesheet array. It is multiplied by 6 because I
  68. //use 7 different sprites for the terrain
  69. Debug.Log(Mathf.RoundToInt(Mathf.PerlinNoise(x / scale, y / scale) * 6));
  70. p = tiles[Mathf.RoundToInt(Mathf.PerlinNoise(x / scale, y / scale) * 6)];
  71. texture.SetPixels(x*tileResolution, y*tileResolution, tileResolution, tileResolution, p);
  72. }
  73. }
  74.  
  75. texture.filterMode = FilterMode.Point;
  76. texture.Apply();
  77.  
  78. MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
  79. mesh_renderer.sharedMaterial.mainTexture = texture;
  80. }
  81.  
  82. public void BuildMesh()
  83. {
  84. int numTiles = size_x * size_z;
  85. int numTris = numTiles * 2;
  86.  
  87. int vsize_x = size_x + 1;
  88. int vsize_z = size_z + 1;
  89. int numVerts = vsize_x * vsize_z;
  90.  
  91. //Generate Mesh Data
  92. Vector3[] vertices = new Vector3[numVerts];
  93. Vector3[] normals = new Vector3[numVerts];
  94. Vector2[] uv = new Vector2[numVerts];
  95.  
  96. int[] triangles = new int[numTris * 3];
  97.  
  98. int x, z;
  99. for (z = 0; z < vsize_z; z++)
  100. {
  101. for (x = 0; x < vsize_x; x++)
  102. {
  103. vertices[z * vsize_x + x] = new Vector3(x*tileSize, 0, z*tileSize);
  104. normals[z * vsize_x + x] = Vector3.up;
  105. uv[z * vsize_x + x] = new Vector2((float)x / size_x, (float)z / size_x);
  106. }
  107. }
  108. for (z = 0; z < size_z; z++)
  109. {
  110. for (x = 0; x < size_x; x++)
  111. {
  112. int squareIndex = z * size_x + x;
  113. int triIndex = squareIndex * 6;
  114.  
  115. triangles[triIndex + 0] = z * vsize_x + x + 0;
  116. triangles[triIndex + 1] = z * vsize_x + x + vsize_x + 0;
  117. triangles[triIndex + 2] = z * vsize_x + x + vsize_x + 1;
  118.  
  119. triangles[triIndex + 3] = z * vsize_x + x + 0;
  120. triangles[triIndex + 4] = z * vsize_x + x + vsize_x + 1;
  121. triangles[triIndex + 5] = z * vsize_x + x + 1;
  122. }
  123. }
  124.  
  125. //Create new mesh and populate it
  126. Mesh mesh = new Mesh();
  127. mesh.vertices = vertices;
  128. mesh.triangles = triangles;
  129. mesh.normals = normals;
  130. mesh.uv = uv;
  131.  
  132. //Assign our mesh to filter/collider
  133. MeshFilter mf = GetComponent<MeshFilter>();
  134. MeshRenderer mr = GetComponent<MeshRenderer>();
  135. MeshCollider mc = GetComponent<MeshCollider>();
  136.  
  137. mf.mesh = mesh;
  138. mc.sharedMesh = mesh;
  139.  
  140. BuildTexture();
  141. }
  142.  
  143. void Update()
  144. {
  145. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  146. RaycastHit hitInfo;
  147.  
  148. texture.Apply();
  149.  
  150. if (GetComponent<Collider>().Raycast(ray, out hitInfo, Mathf.Infinity))
  151. {
  152. int coordX = Mathf.FloorToInt(hitInfo.point.x / tileSize);
  153. int coordZ = Mathf.FloorToInt(hitInfo.point.z / tileSize);
  154.  
  155. currentTileCoord.x = coordX;
  156. currentTileCoord.z = coordZ;
  157.  
  158. selectionCube.transform.position = currentTileCoord * tileSize;
  159.  
  160. q = tiles[1];
  161.  
  162. if (Input.GetMouseButton(0))
  163. {
  164. texture.SetPixels(coordX * tileResolution, coordZ * tileResolution, tileResolution, tileResolution, q);
  165. }
  166. }
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement