public static Cell Create(Vector2 index, MapManager map) { var name = "(" + index.x + "," + index.y + ")"; var go = new GameObject(name); var mf = go.AddComponent <MeshFilter>(); mf.mesh = CreatePlane.Create(name, map.SizeOfCell, map.SizeOfCell, map.NumberOfTiles, map.NumberOfTiles); go.AddComponent <MeshRenderer>(); go.AddComponent <MeshCollider>(); var cell = go.AddComponent <Cell>(); cell.GenerateTiles(map.NumberOfTiles); go.transform.parent = map.transform; // cellPosition is the position in the world of the cell var cellPosition = index; cellPosition *= map.SizeOfCell; cellPosition.x -= map.HalfTileSize; cellPosition.y -= map.HalfTileSize; go.transform.position = new Vector3(cellPosition.x, map.transform.position.y, cellPosition.y); go.layer = LayerMask.NameToLayer("LevelTerrain"); var t = new Texture2D(map.TileResolution * map.NumberOfTiles, map.TileResolution * map.NumberOfTiles); var texName = "TEX" + name; t.name = texName; var transparent = new Color[map.TileResolution * map.NumberOfTiles * map.TileResolution * map.NumberOfTiles]; var c = new Color(0, 0, 0, 0); for (var y = 0; y < map.TileResolution * map.NumberOfTiles; y++) { for (var x = 0; x < map.TileResolution * map.NumberOfTiles; x++) { transparent[y * map.TileResolution * map.NumberOfTiles + x] = c; } } t.SetPixels(transparent); t.wrapMode = TextureWrapMode.Clamp; t.Apply(); var shader = Shader.Find(map.DefaultShader); if (shader == null) { shader = Shader.Find("Diffuse"); } var m = new Material(shader); var matName = "MAT" + name; m.name = matName; m.mainTexture = t; go.GetComponent <Renderer>().sharedMaterial = m; var me = new MapEditing(map); me.UpdateCellFromNeighbours(cell); me.Apply(); return(cell); }
public static Cell Create(Point p, TilerMap tilerMap) { var cellName = GetName(p); var go = new GameObject(cellName); go.isStatic = true; var tr = go.transform; tr.parent = tilerMap.transform; var position = new Vector3 { x = p.X * tilerMap.CellSize + tilerMap.TileSize / 2f, y = 0, z = p.Y * tilerMap.CellSize + tilerMap.TileSize / 2f }; tr.localPosition = position; var mf = go.AddComponent <MeshFilter>(); if (tilerMap.SharedMesh == null) { tilerMap.SharedMesh = CreatePlane.Create("_MESH", tilerMap.CellSize, tilerMap.CellSize); } mf.sharedMesh = tilerMap.SharedMesh; var mr = go.AddComponent <MeshRenderer>(); var bc = go.AddComponent <BoxCollider>(); bc.size = new Vector3(tilerMap.CellSize, 0, tilerMap.CellSize); var t = new Texture2D(tilerMap.TextureResolution, tilerMap.TextureResolution, TextureFormat.ARGB32, true); var texName = cellName + "_TEX"; t.name = texName; t.wrapMode = TextureWrapMode.Clamp; var c = Util.InitilizeArray(tilerMap.TextureResolution, new Color32(205, 205, 205, 0)); t.SetPixels32(c); t.Apply(); var shader = Shader.Find(tilerMap.DefaultShader); var m = new Material(shader); var matName = cellName + "_MAT"; m.name = matName; m.mainTexture = t; m.renderQueue -= tilerMap.Layer; mr.sharedMaterial = m; var cell = go.AddComponent <Cell>(); cell.Setup(tilerMap.TilesPerCell); return(cell); }