Unity AssetBundle 之 (进阶)简单的实现 一键删除打包好的 AssetBundle 包资源 的方法
目录
Unity AssetBundle 之 (进阶)简单的实现 一键删除打包好的 AssetBundle 包资源 的方法
一、简单介绍
Unity中的一些基础知识点。
本节介绍,Asset Bundle 在 Unity中的使用,进阶第四篇,可能需要重新打 Asset Bundle 包,所有需要删除之前打的 AssetBundle 包的方法,有不对的地方欢迎指正。
二、实现原理
1、使用 Directory.Delete() 删除打包的文件夹, File.Delete() 删除对应的.meta 文件
三、注意事项
1、AssetDatabase.Refresh() 刷新文件夹,不然可能看不到效果
四、效果预览
五、实现步骤
1、打开Unity ,新建一个空工程
2、把资源导进来,这里 AssetBundle 标记标签的文件夹为 AB_Resources,文件结构如下,需要标记的资源如图功能文件夹放置
3、新建脚本 DeleteAssetBundle,用来删除以打包的Asset Bundle资源,PathTools 一些 AB 路径的管理脚本
4、菜单栏就会有 AssetBundleTools -- DeleteAllAssetBundles 工具了
5、删除打包好的Asset Bundle资源,前提是要有已经打包好的AB包
可参看以下博客进行 Asset Bundle 一键打包
Unity AssetBundle 之 (进阶)简单的实现不同平台给 AB 标签的资源打包 Asset Bundle 包(分平台)的方法
6、点击 DeleteAllAssetBundles 就可以一键删除了
六、关键代码
1、DeleteAssetBundle
/****************************************************
文件:DeleteAssetBundle.cs
作者:Administrator
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/14 19:10:45
功能:Nothing
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace ABFw
{
public class DeleteAssetBundle
{
[MenuItem("AssetBundleTools/DeleteAllAssetBundles")]
public static void DelAssetBundle() {
// 删除AB包输出目录
string strNeedDeleteDir = string.Empty;
strNeedDeleteDir = PathTools.GetABOutPath();
if (string.IsNullOrEmpty(strNeedDeleteDir) == false)
{
// 注意 :这里参数 true 表示可以删除非空目录
Directory.Delete(strNeedDeleteDir,true);
// 同时删除 meta 文件(去除不必要的警告)
File.Delete(strNeedDeleteDir + ".meta");
// 刷新目录
AssetDatabase.Refresh();
}
}
}//class_End
}
2、PathTools
/****************************************************
文件:PathTools.cs
作者:仙魁XAN
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/14 17:11:52
功能:AB框架的 路径工具类
1、路径常量
2、路径方法
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace ABFw
{
public class PathTools
{
/* 路径常量 */
public const string AB_RESOURCES = "AB_Resources"; // 打包AB包根路径
/* 路径方法 */
/// <summary>
/// 得到 AB 资源的输入目录
/// </summary>
/// <returns></returns>
public static string GetABResourcesPath()
{
return Application.dataPath + "/" + AB_RESOURCES;
}
/// <summary>
/// 获得 AB 包输出路径
/// 1\ 平台(PC/移动端等)路径
/// 2\ 平台名称
/// </summary>
/// <returns></returns>
public static string GetABOutPath()
{
return GetPlatformPath() + "/" + GetPlatformName();
}
/// <summary>
/// 获得平台路径
/// </summary>
/// <returns></returns>
private static string GetPlatformPath()
{
string strReturenPlatformPath = string.Empty;
#if UNITY_STANDALONE_WIN
strReturenPlatformPath = Application.streamingAssetsPath;
#elif UNITY_IPHONE
strReturenPlatformPath = Application.persistentDataPath;
#elif UNITY_ANDROID
strReturenPlatformPath = Application.persistentDataPath;
#endif
return strReturenPlatformPath;
}
/// <summary>
/// 获得平台名称
/// </summary>
/// <returns></returns>
public static string GetPlatformName()
{
string strReturenPlatformName = string.Empty;
#if UNITY_STANDALONE_WIN
strReturenPlatformName = "Windows";
#elif UNITY_IPHONE
strReturenPlatformName = "IPhone";
#elif UNITY_ANDROID
strReturenPlatformName = "Android";
#endif
return strReturenPlatformName;
}
/// <summary>
/// 返回 WWW 下载 AB 包加载路径
/// </summary>
/// <returns></returns>
public static string GetWWWAssetBundlePath()
{
string strReturnWWWPath = string.Empty;
#if UNITY_STANDALONE_WIN
strReturnWWWPath = "file://" + GetABOutPath();
#elif UNITY_IPHONE
strReturnWWWPath = GetABOutPath() + "/Raw/";
#elif UNITY_ANDROID
strReturnWWWPath = "jar:file://" + GetABOutPath();
#endif
return strReturnWWWPath;
}
}//Class_End
}