private void SetTileObjTransform(GameObject tile_gameObject, int grid_x, int grid_y, TilePrefabData tilePrefabData, uint tileData) { // 比如格子[0,0],中点坐标应该是[0.5 * cell_size.x,0.5 * cell_size.y] Vector3 chunk_localPosition = new Vector3((grid_x + .5f) * cell_size.x, (grid_y + .5f) * cell_size.y, tilePrefabData.prefab.transform.position.z); if (tilePrefabData.offsetMode == TilePrefabDataOffsetMode.Pixels) { float ppu = tileSet.tile_pixel_size.x / cell_size.x; chunk_localPosition += tilePrefabData.offset / ppu; } else //if (tilePrefabData.offsetMode == TileOffsetMode.Unit) { chunk_localPosition += tilePrefabData.offset; } Vector3 worldPosition = transform.TransformPoint(chunk_localPosition); tile_gameObject.transform.position = worldPosition; tile_gameObject.transform.rotation = transform.rotation; tile_gameObject.transform.parent = transform.parent; tile_gameObject.transform.localRotation = tilePrefabData.prefab.transform.localRotation; tile_gameObject.transform.localScale = tilePrefabData.prefab.transform.localScale; //+++ Apply tile flags Vector3 localScale = tile_gameObject.transform.localScale; if (TileSetUtil.IsTileFlagRot90(tileData)) { tile_gameObject.transform.localRotation *= Quaternion.Euler(0, 0, -90); } //For Rot180 and Rot270 avoid changing the scale if (TileSetUtil.IsTileFlagFlipH(tileData) && TileSetUtil.IsTileFlagFlipV(tileData)) { tile_gameObject.transform.localRotation *= Quaternion.Euler(0, 0, -180); } else { if (TileSetUtil.IsTileFlagFlipH(tileData)) { localScale.x = -tile_gameObject.transform.localScale.x; } if (TileSetUtil.IsTileFlagFlipV(tileData)) { localScale.y = -tile_gameObject.transform.localScale.y; } } tile_gameObject.transform.localScale = localScale; }
//在指定的dst_rect区域画tile public static void DoGUIDrawTileFromTileData(Rect dst_rect, uint tileData, TileSet tileSet, Rect custom_uv = default(Rect)) { int tileId = (int)(tileData & TileSetConst.TileDataMask_TileId); Tile tile = tileSet.GetTile(tileId); if (tileId != TileSetConst.TileId_Empty && tileSet.atlas_texture) { if (TileSetUtil.IsTileFlagFlipV(tileData)) { GUIUtility.ScaleAroundPivot(new Vector2(1f, -1f), dst_rect.center); } if (TileSetUtil.IsTileFlagFlipH(tileData)) { GUIUtility.ScaleAroundPivot(new Vector2(-1f, 1f), dst_rect.center); } if (TileSetUtil.IsTileFlagRot90(tileData)) { GUIUtility.RotateAroundPivot(90f, dst_rect.center); } if (tile != null && tile.tilePrefabData.prefab && tile.tilePrefabData.is_show_prefab_preview_in_tile_palette) { Texture2D asset_preview = AssetPreview.GetAssetPreview(tile.tilePrefabData.prefab); if (asset_preview) { GUI.DrawTexture(dst_rect, asset_preview, ScaleMode.ScaleToFit); } else { GUI.DrawTextureWithTexCoords(dst_rect, tileSet.atlas_texture, custom_uv == default(Rect) && tile != null ? tile.uv : custom_uv, true); } } else { GUI.DrawTextureWithTexCoords(dst_rect, tileSet.atlas_texture, custom_uv == default(Rect) && tile != null ? tile.uv : custom_uv, true); } GUI.matrix = Matrix4x4.identity; } }