private TileMapChunk GetOrCreateTileMapChunk(int world_grid_x, int world_grid_y, bool is_create_if_doesnt_exist = false) { if (tileMapChunk_cache_dict.Count == 0 && transform.childCount > 0) { BuildTileMapChunkCacheDict(); } Vector2Int chunk_xy = TileMapChunkUtil.GetChunkXY(world_grid_x, world_grid_y); TileMapChunk tileMapChunk = null; uint key = GetTileMapChunkKey(chunk_xy); tileMapChunk_cache_dict.TryGetValue(key, out tileMapChunk); if (tileMapChunk == null && is_create_if_doesnt_exist) { string chunk_name = string.Format("{0}_{1}", chunk_xy.x, chunk_xy.y); GameObject chunk_gameObject = new GameObject(chunk_name); if (is_undo_enabled) { #if UNITY_EDITOR Undo.RegisterCreatedObjectUndo(chunk_gameObject, TileMapConst.Undo_Operation_Name + name); #endif } tileMapChunk = chunk_gameObject.AddComponent <TileMapChunk>(); chunk_gameObject.transform.parent = transform; Vector2Int offset_grid_xy = TileMapChunkUtil.GetOffsetGridXY(chunk_xy); tileMapChunk.offset_grid_x = offset_grid_xy.x; tileMapChunk.offset_grid_y = offset_grid_xy.y; chunk_gameObject.transform.localPosition = new Vector2(tileMapChunk.offset_grid_x * cell_size.x, tileMapChunk.offset_grid_y * cell_size.y); chunk_gameObject.transform.localRotation = Quaternion.identity; chunk_gameObject.transform.localScale = Vector3.one; chunk_gameObject.hideFlags = gameObject.hideFlags | HideFlags.HideInHierarchy; if (Application.isPlaying) { tileMapChunk.Reset(); } tileMapChunk.parent_tileMap = this; tileMapChunk.SetDimensions(TileMapConst.TileMapChunk_Size, TileMapConst.TileMapChunk_Size); tileMapChunk.SetSharedMaterial(material); tileMapChunk.SortingLayerID = sortingLayer; tileMapChunk.OrderInLayer = orderInLayer; tileMapChunk.UpdateRendererProperties(); tileMapChunk_cache_dict[key] = tileMapChunk; } return(tileMapChunk); }
public uint GetTileData(int world_grid_x, int world_grid_y) { TileMapChunk tileMapChunk = GetOrCreateTileMapChunk(world_grid_x, world_grid_y); if (tileMapChunk == null) { return(TileSetConst.TileData_Empty); } else { Vector2Int local_grid_xy = TileMapChunkUtil.GetLocalGridXYOfChunk(world_grid_x, world_grid_y); int local_grid_x = local_grid_xy.x; int local_grid_y = local_grid_xy.y; if (world_grid_x < 0) { local_grid_x = TileMapConst.TileMapChunk_Size - 1 - local_grid_x; } if (world_grid_y < 0) { local_grid_y = TileMapConst.TileMapChunk_Size - 1 - local_grid_y; } return(tileMapChunk.GetTileData(local_grid_x, local_grid_y)); } }
//在chunk数组中的[][]坐标中x,chunck数组与grid格子数组类似,(只不过是grid_x/TileMapConst.TileMapChunk_Size,所以里面的元素个数相对于grid格子数组里面的个数少) public Vector2Int GetChunkXY() { return(TileMapChunkUtil.GetChunkXY(offset_grid_x, offset_grid_y)); }