public void RefreshTileObjects() { // 销毁tile为null或者prefab为null的object for (int i = 0; i < tileObjData_list.Count; ++i) { TileObjData tileObjData = tileObjData_list[i]; uint tileData = tileData_list[tileObjData.index]; int tileId = TileSetUtil.GetTileIdFromTileData(tileData); Tile tile = tileSet.GetTile(tileId); if (tile == null || tile.tilePrefabData.prefab == null) { DestroyTileObject(tileObjData.index); } } //重建 for (int tile_index = 0; tile_index < tileData_list.Count; ++tile_index) { uint tileData = tileData_list[tile_index]; int tileId = TileSetUtil.GetTileIdFromTileData(tileData); Tile tile = tileSet.GetTile(tileId); if (tile != null && tile.tilePrefabData.prefab != null) { CreateTileObject(tile_index, tile.tilePrefabData); } } }
private void DestroyTileObject(int tile_index) { TileObjData tileObjData = FindTileObjDataByTileIndex(tile_index); if (tileObjData != null) { tile_gameObject_to_be_removed_list.Add(tileObjData.gameObject); tileObjData_list.Remove(tileObjData); } }
private TileObjData FindTileObjDataByTileIndex(int tile_index) { for (int i = 0; i < tileObjData_list.Count; ++i) { TileObjData tileObjData = tileObjData_list[i]; if (tileObjData.index == tile_index) { return(tileObjData); } } return(null); }
private GameObject CreateTileObject(int tile_index, TilePrefabData tilePrefabData) { if (tilePrefabData.prefab != null) { TileObjData tileObjData = FindTileObjDataByTileIndex(tile_index); GameObject tile_gameObject = null; int grid_x = tile_index % width; int grid_y = tile_index / width; if (tileObjData == null || tileObjData.tilePrefabData != tilePrefabData || tileObjData.gameObject == null) { #if UNITY_EDITOR tile_gameObject = (GameObject)PrefabUtility.InstantiatePrefab(tilePrefabData.prefab); // allow destroy the object with undo operations if (parent_tileMap.is_undo_enabled) { Undo.RegisterCreatedObjectUndo(tile_gameObject, TileMapConst.Undo_Operation_Name + parent_tileMap.name); } #else tile_gameObject = (GameObject)Instantiate(tilePrefabData.prefab, Vector3.zero, transform.rotation); #endif SetTileObjTransform(tile_gameObject, grid_x, grid_y, tilePrefabData, tileData_list[tile_index]); if (tileObjData != null) { tile_gameObject_to_be_removed_list.Add(tileObjData.gameObject); tileObjData.gameObject = tile_gameObject; tileObjData.tilePrefabData = tilePrefabData; } else { tileObjData_list.Add(new TileObjData() { index = tile_index, gameObject = tile_gameObject, tilePrefabData = tilePrefabData }); } tile_gameObject.SendMessage(TileMapConst.OnTilePrefabCreation, new OnTilePrefabCreationData() { parent_tileMap = parent_tileMap, grid_x = offset_grid_x + grid_x, grid_y = offset_grid_y + grid_y }, SendMessageOptions.DontRequireReceiver); return(tile_gameObject); } else if (tileObjData.gameObject != null) { #if UNITY_EDITOR //+++ Break tilemap prefab and restore tile prefab link GameObject parentPrefab = PrefabUtility.FindRootGameObjectWithSameParentPrefab(tileObjData.gameObject); if (parentPrefab != tileObjData.gameObject) { DestroyImmediate(tileObjData.gameObject); tileObjData.gameObject = PrefabUtility.InstantiatePrefab(tileObjData.tilePrefabData.prefab) as GameObject; } #endif SetTileObjTransform(tileObjData.gameObject, grid_x, grid_y, tilePrefabData, tileData_list[tile_index]); tileObjData.gameObject.SendMessage(TileMapConst.OnTilePrefabCreation, new OnTilePrefabCreationData() { parent_tileMap = parent_tileMap, grid_x = offset_grid_x + grid_x, grid_y = offset_grid_y + grid_y }, SendMessageOptions.DontRequireReceiver); return(tileObjData.gameObject); } } return(null); }