public void SetIndex(int indexTile) { this.data.tileLayer.layer1 = indexTile; bool isEnabled = !(indexTile == -1 || indexTile == 255); meshRenderer.enabled = (isEnabled); if (isEnabled) { try { TilesetManager.Tileset tileset = TilesetManager.instance.LayerTileset(0); material.SetTexture("_MainTex", tileset.tiles[indexTile].texture); } catch (System.Exception) { throw; } } }
public void CreateNewMap() { bool isChecked = true; if (!inpSizeX.Validate()) { isChecked = false; } if (!inpSizeY.Validate()) { isChecked = false; } if (isChecked) { if (currentMap != null) { Debug.Log("Existing map"); Destroy(currentMap.gameObject); } GameObject inst = new GameObject("MapDataModif"); inst.transform.parent = this.transform; currentMap = inst.AddComponent <MapDataMof>(); currentMap.isMapEditor = true; int sizeX = 1; int sizeY = 1; int.TryParse(inpSizeX.Content, out sizeX); int.TryParse(inpSizeY.Content, out sizeY); if (mTypeMap == 2) { Debug.Log("NotDoneYet"); } Material materialSelected = null; TilesetManager.Tileset tilesetTarget = null; switch (mTypeMap) { case 1: materialSelected = tilesetMat; tilesetTarget = tilesetTile; break; case 2: materialSelected = chunckMat; tilesetTarget = tilesetChunck; break; case 3: materialSelected = fullMat; tilesetTarget = tilesetFull; break; default: break; } //currentMap.CreateMap(new Vector2Int(sizeX, sizeY), tilesetMat); TilesetManager.instance.SetTileset(0, tilesetTarget); currentMap.CreateEditorMap(new Vector2Int(sizeX, sizeY), mTypeMap, materialSelected); MenuManager.Instance.ActiveState(EMenuState.Editor_Menu); UpdateToolsButtons(); CenterMap(); } }
public void GenerateMap(JsonMapData jsonMapData, Material tilesetMat) { sizeMap = jsonMapData.sizeMap; GameObject chunck = new GameObject("Chunck"); chunck.transform.parent = this.transform; MeshFilter meshFilter = chunck.AddComponent <MeshFilter>(); MeshRenderer meshRenderer = chunck.AddComponent <MeshRenderer>(); int numberVertices = (jsonMapData.sizeMap.x) * (jsonMapData.sizeMap.y) * 4; int numberTriangles = (jsonMapData.sizeMap.x) * (jsonMapData.sizeMap.y) * 6; Vector3[] vertices = new Vector3[numberVertices]; Vector2[] uvs = new Vector2[numberVertices]; int[] triangles = new int[numberTriangles]; int indexTriangles = 0; mapData = new List <TileData>(); for (int y = 0; y < jsonMapData.sizeMap.y; y++) { for (int x = 0; x < jsonMapData.sizeMap.x; x++) { int index = (jsonMapData.sizeMap.x * 2) * (y * 2) + (x * 2); int indexMap = (jsonMapData.sizeMap.x) * y + x; int indexTile = jsonMapData.tileData[indexMap].tileLayer.layer1; if (indexTile != -1 && indexTile != 255) { TilesetManager.Tileset tileset = TilesetManager.instance.LayerTileset(0); TilesetManager.Tileset.Tile tile = tileset.tiles[indexTile]; GameObject goTile = new GameObject("Tile " + indexMap); goTile.transform.parent = this.transform; TileData tileData = goTile.AddComponent <TileData>(); tileData.data.indexMap = indexMap; tileData.data.tileLayer.layer1 = indexTile; mapData.Add(tileData); //UL int newIndex = index; vertices[newIndex] = new Vector3(x, 0, y); uvs[newIndex] = tile.uv_UL; //uvs[newIndex] = new Vector2(x, y); //UR newIndex = index + 1; vertices[newIndex] = new Vector3(x + 1, 0, y); uvs[newIndex] = tile.uv_UR; //uvs[newIndex] = new Vector2((x + 1), y); //DL newIndex = index + (jsonMapData.sizeMap.x * 2); vertices[newIndex] = new Vector3(x, 0, y + 1); uvs[newIndex] = tile.uv_DL; //uvs[newIndex] = new Vector2(x, (y + 1)); //DR newIndex = index + (jsonMapData.sizeMap.x * 2) + 1; vertices[newIndex] = new Vector3(x + 1, 0, y + 1); uvs[newIndex] = tile.uv_DR; //uvs[newIndex] = new Vector2((x + 1), (y + 1)); } triangles[indexTriangles + 0] = index; triangles[indexTriangles + 1] = index + (jsonMapData.sizeMap.x * 2); triangles[indexTriangles + 2] = index + 1; triangles[indexTriangles + 3] = index + (jsonMapData.sizeMap.x * 2); triangles[indexTriangles + 4] = index + (jsonMapData.sizeMap.x * 2) + 1; triangles[indexTriangles + 5] = index + 1; indexTriangles += 6; } } Mesh mesh = new Mesh(); mesh.name = "ChunckMesh"; mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; mesh.RecalculateNormals(); meshFilter.sharedMesh = mesh; meshRenderer.sharedMaterial = tilesetMat; }