private void OnButtonDelete() { if (this.tilesetRecord.Tileset != null) { DeleteTilesetFlag flags = 0; if (this.shouldDeleteAtlasTexture) { flags |= DeleteTilesetFlag.DeleteTexture; } if (this.shouldDeleteAtlasMaterial) { flags |= DeleteTilesetFlag.DeleteMaterial; } if (this.shouldDeleteMeshes) { flags |= DeleteTilesetFlag.DeleteMeshAssets; } BrushUtility.DeleteTileset(this.tilesetRecord.Tileset, flags); } this.Close(); }
/// <summary> /// Delete tileset and associated assets. /// </summary> /// <remarks> /// <para>Can only delete tileset assets of type <see cref="Tileset"/> /// or <see cref="AutotileTileset"/>.</para> /// </remarks> /// <param name="tileset">The tileset.</param> /// <param name="flags">Optional deletion flags.</param> /// <exception cref="System.ArgumentNullException"> /// If <paramref name="tileset"/> is <c>null</c>. /// </exception> /// <exception cref="System.ArgumentException"> /// If attempting to delete an unsuported type of tileset. /// </exception> /// <exception cref="System.Exception"> /// If tileset record was not found for specified tileset. /// </exception> public static void DeleteTileset(Tileset tileset, DeleteTilesetFlag flags = 0) { if (tileset == null) { throw new ArgumentNullException("tileset"); } Type tilesetType = tileset.GetType(); if (tilesetType != typeof(Tileset) && tilesetType != typeof(AutotileTileset)) { throw new ArgumentException("This utility function cannot delete tileset of type '" + tilesetType.FullName + "'", "tileset"); } var tilesetRecord = BrushDatabase.Instance.FindTilesetRecord(tileset); if (tilesetRecord == null) { throw new Exception("Tileset record was not found for '" + tileset.name + "'"); } var autotileTileset = tileset as AutotileTileset; string assetPath = tilesetRecord.AssetPath; string assetFolderPath = assetPath.Substring(0, assetPath.LastIndexOf("/")); string assetFolderPathBase = assetFolderPath + "/"; int slashCount = assetFolderPathBase.CountSubstrings('/'); // Note: Only delete assets where: // - Asset path begins with base path of tileset asset. // - Asset path includes the same number of slashes (to avoid deleting // those that are nested within folders). // Delete associated texture? if (autotileTileset != null && (flags & DeleteTilesetFlag.DeleteTexture) == DeleteTilesetFlag.DeleteTexture && autotileTileset.AtlasTexture != null) { string t = AssetDatabase.GetAssetPath(tileset.AtlasTexture); if (t.StartsWith(assetFolderPathBase) && slashCount == t.CountSubstrings('/')) { AssetDatabase.DeleteAsset(t); } } // Delete associated material? if ((flags & DeleteTilesetFlag.DeleteMaterial) == DeleteTilesetFlag.DeleteMaterial && tileset.AtlasMaterial != null) { string t = AssetDatabase.GetAssetPath(tileset.AtlasMaterial); if (t.StartsWith(assetFolderPathBase) && slashCount == t.CountSubstrings('/')) { AssetDatabase.DeleteAsset(t); } } // Delete associated mesh assets? if ((flags & DeleteTilesetFlag.DeleteMeshAssets) == DeleteTilesetFlag.DeleteMeshAssets && tileset.tileMeshAsset != null) { string t = AssetDatabase.GetAssetPath(tileset.tileMeshAsset); if (t.StartsWith(assetFolderPathBase) && slashCount == t.CountSubstrings('/')) { AssetDatabase.DeleteAsset(t); } } // Delete tileset and contained brush assets. AssetDatabase.DeleteAsset(assetPath); // Delete tileset folder if it is empty. if (IsDirectoryEmpty(Directory.GetCurrentDirectory() + "/" + assetFolderPath)) { AssetDatabase.DeleteAsset(assetFolderPath); } ToolUtility.RepaintBrushPalette(); DesignerWindow.RepaintWindow(); }