/// <summary> /// Mapcell2s the index of the area.取得大图的index映射到分区网格的index /// </summary> /// <returns>The area index.</returns> /// <param name="grid">Grid.</param> /// <param name="gridArea">Grid area.</param> /// <param name="index">Index.</param> /// <param name="scale">Scale.</param> int mapIndex2AreaIndex(GridBase grid, GridBase gridArea, int index, int scale) { int areaIndex = -1; int col = grid.GetColumn(index); int row = grid.GetRow(index); col = col / scale; row = row / scale; areaIndex = gridArea.GetCellIndex(col, row); return(areaIndex); }
/// 分区网格的index转成大地图每屏的index List <int> areaIndex2MapPageIndexs(GridBase grid, GridBase gridArea, int areaIndex, int scale) { List <int> ret = new List <int>(); int col = gridArea.GetColumn(areaIndex); int row = gridArea.GetRow(areaIndex); col = col * scale; row = row * scale; for (int i = col + editorCfg.pageSize / 2; i < col + scale - 1; i = i + editorCfg.pageSize) { for (int j = row + editorCfg.pageSize / 2; j < row + scale - 1; j = j + editorCfg.pageSize) { ret.Add(grid.GetCellIndex(i, j)); } } return(ret); }
void generateData() { if (editorCfg == null) { Debug.Log("Editor config is null"); return; } // 设置grid GridBase grid = new GridBase(); grid.init(Vector3.zero, editorCfg.size, editorCfg.size, 1); // 先把图的格式设置成RGBA32 Texture2D areaTex = editorCfg.oceanGidTexture; if (areaTex == null) { Debug.Log("oceanGid Texture is null"); return; } // 先取得大地图分区的数据 Color32[] colors = areaTex.GetPixels32(); Debug.Log("areaTex.width===" + areaTex.width); Debug.Log("colors.Length===" + colors.Length); // 区域块的index对应的val Hashtable areaMap = new Hashtable(); // 取得大地图与分区地图的比例,以例可以对换 int mapareaScale = editorCfg.size / areaTex.width; //地图分区块的网格 GridBase gridArea = new GridBase(); gridArea.init(Vector3.zero, areaTex.width, areaTex.width, mapareaScale); Color c; for (int i = 0; i < colors.Length - 1; i++) { c = colors[i]; int areaVal = 0; if (editorCfg.colors4MapArea.TryGetValue(c, out areaVal)) { areaMap[i] = areaVal; } else { Debug.LogError("get area val by color is nil!" + c + "==" + c * 255); } } //地图数据 Hashtable mapInfor = new Hashtable(); // 生成据点 int tmpSize = editorCfg.pageSize * 2; for (int i = tmpSize / 2; i < editorCfg.size; i += tmpSize) { for (int j = tmpSize / 2; j < editorCfg.size; j += tmpSize) { int index = grid.GetCellIndex(i, j); List <int> cells = grid.getCells(index, 5); int k = NumEx.NextInt(0, cells.Count); while (true) { if (k >= cells.Count) { k = 0; } if (mapInfor[(int)(cells[k])] == null) { mapInfor[(int)(cells[k])] = 1; break; } k++; } } } // 生成装饰 for (int i = editorCfg.pageSize / 2; i < editorCfg.size; i = i + editorCfg.pageSize) { for (int j = editorCfg.pageSize / 2; j < editorCfg.size; j = j + editorCfg.pageSize) { int index = grid.GetCellIndex(i, j); switch (NumEx.NextInt(0, 4)) { case 0: addIsland1(grid, index, mapInfor); break; case 1: addIsland2(grid, index, mapInfor); break; case 2: addIsland3(grid, index, mapInfor); break; default: addIsland4(grid, index, mapInfor); break; } } } //导出数据(会根据分区导出数据,这里会导出100份数据,方便加载) /* * 每个分区id对应一个文件,每个文件里存一个table,key是一屏的index,value是table2 * table2里的key是网格index,value是地块配置id */ string path = Application.dataPath + "/" + CLPathCfg.self.basePath + "/" + CLPathCfg.upgradeRes + "/priority/cfg/worldmap/maparea.cfg"; Directory.CreateDirectory(Path.GetDirectoryName(path)); MemoryStream ms = new MemoryStream(); B2OutputStream.writeObject(ms, areaMap); File.WriteAllBytes(path, ms.ToArray()); Debug.Log(path); for (int i = 0; i < areaTex.width; i++) { for (int j = 0; j < areaTex.width; j++) { int index = i * areaTex.width + j; Hashtable areaPageMap = new Hashtable(); List <int> pagesIndes = areaIndex2MapPageIndexs(grid, gridArea, index, mapareaScale); for (int p = 0; p < pagesIndes.Count; p++) { int center = pagesIndes[p]; List <int> cells = grid.getCells(center, editorCfg.pageSize); Hashtable map = new Hashtable(); for (int k = 0; k < cells.Count; k++) { if (mapInfor[cells[k]] != null) { map[cells[k]] = mapInfor[cells[k]]; } } areaPageMap[center] = map; } path = Application.dataPath + "/" + CLPathCfg.self.basePath + "/" + CLPathCfg.upgradeRes + "/priority/cfg/worldmap/" + index + ".cfg"; Directory.CreateDirectory(Path.GetDirectoryName(path)); ms = new MemoryStream(); B2OutputStream.writeObject(ms, areaPageMap); File.WriteAllBytes(path, ms.ToArray()); } } Debug.Log("total===" + mapInfor.Count); }