Unity AssetBundle 之 (进阶)简单的自动 清理/移除 指定目录下的资源上 AssetBundle 的标签 的方法
目录
Unity AssetBundle 之 (进阶)简单的自动 清理/移除 指定目录下的资源上 AssetBundle 的标签 的方法
一、简单介绍
Unity中的一些基础知识点。
本节介绍,Asset Bundle 在 Unity中的使用,进阶第三篇,给已经打上 AB 标签的资源,自动清理或者移除掉资源上的AB标签的方法,有不对的地方欢迎指正。
二、实现原理
1、其实与自动打AB标签的方法类似,把标签改为 string.Empty 即可
2、使用 Editor 脚本,指定资源文件夹,AssetImporter 进行移除资源上 AssetBundle 标记的标签
三、注意事项
1、脚本一定要放在 Editor 文件夹下
四、效果预览
五、实现步骤
1、打开Unity ,新建一个空工程
2、把资源导进来,这里 AssetBundle 标记标签的文件夹为 AB_Resources,文件结构如下,需要标记的资源如图功能文件夹放置
3、新建脚本 AutoRemoveAssetBundleLabel,用来给标签了的资源移除AB标签,PathTools 一些 AB 路径的管理脚本
4、菜单栏就会有 AssetBundleTools -- Remove AB Label工具了
5、清理移除资源的AB标签,要提前给资源打上AB 标签
可以参看这篇博文,自动给资源打上 AB 标签
Unity AssetBundle 之 (进阶)简单的自动给资源打上 AssetBundle 标签(分平台),方便 AssetBundle 打包的方法
6、点击 Remove AB Label 就会自动移除指定文件夹下资源的 AB 标签了
六、关键代码
1、AutoRemoveAssetBundleLabel
/****************************************************
文件:AutoRemoveAssetBundleLabel.cs
作者:Administrator
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/16 13:37:54
功能:移除资源已经AB标签
1、从打过AB标签的根目录开始
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace ABFw
{
public class AutoRemoveAssetBundleLabel
{
[MenuItem("AssetBundleTools/Remove AB Label")]
public static void RemoveABLabel()
{
// 需要移除标记的根目录
string strNeedRemoveLabelRoot = string.Empty;
// 目录信息(场景目录信息数组,表示所有根目录下场景目录)
DirectoryInfo[] directoryDIRArray = null;
// 定义需要移除AB标签的资源的文件夹根目录
strNeedRemoveLabelRoot = PathTools.GetABResourcesPath();
//Debug.Log("strNeedSetLabelRoot = "+strNeedSetLabelRoot);
DirectoryInfo dirTempInfo = new DirectoryInfo(strNeedRemoveLabelRoot);
directoryDIRArray = dirTempInfo.GetDirectories();
// 遍历本场景目录下所有的目录或者文件
foreach (DirectoryInfo currentDir in directoryDIRArray)
{
// 递归调用方法,找到文件,则使用 AssetImporter 类,标记“包名”与 “后缀名”
JudgeDirOrFileByRecursive(currentDir);
}
// 清空无用的 AB 标记
AssetDatabase.RemoveUnusedAssetBundleNames();
// 刷新
AssetDatabase.Refresh();
// 提示信息,标记包名完成
Debug.Log("AssetBundle 本次操作移除标记完成");
}
/// <summary>
/// 递归判断判断是否是目录或文件
/// 是文件,修改 Asset Bundle 标记
/// 是目录,则继续递归
/// </summary>
/// <param name="fileSystemInfo">当前文件信息(文件信息与目录信息可以相互转换)</param>
private static void JudgeDirOrFileByRecursive(FileSystemInfo fileSystemInfo)
{
// 调试信息
//Debug.Log("currentDir.Name = " + fileSystemInfo.Name);
//Debug.Log("sceneName = " + sceneName);
// 参数检查
if (fileSystemInfo.Exists == false)
{
Debug.LogError("文件或者目录名称:" + fileSystemInfo + " 不存在,请检查");
return;
}
// 得到当前目录下一级的文件信息集合
DirectoryInfo directoryInfoObj = fileSystemInfo as DirectoryInfo; // 文件信息转为目录信息
FileSystemInfo[] fileSystemInfoArray = directoryInfoObj.GetFileSystemInfos();
foreach (FileSystemInfo fileInfo in fileSystemInfoArray)
{
FileInfo fileInfoObj = fileInfo as FileInfo;
// 文件类型
if (fileInfoObj != null)
{
// 修改此文件的 AssetBundle 标签
RemoveFileABLabel(fileInfoObj);
}
// 目录类型
else
{
// 如果是目录,则递归调用
JudgeDirOrFileByRecursive(fileInfo);
}
}
}
/// <summary>
/// 给文件移除 Asset Bundle 标记
/// </summary>
/// <param name="fileInfoObj">文件(文件信息)</param>
static void RemoveFileABLabel(FileInfo fileInfoObj)
{
// 调试信息
//Debug.Log("fileInfoObj.Name = " + fileInfoObj.Name);
//Debug.Log("scenesName = " + scenesName);
// 参数定义
// AssetBundle 包名称
string strABName = string.Empty;
// 文件路径(相对路径)
string strAssetFilePath = string.Empty;
// 参数检查(*.meta 文件不做处理)
if (fileInfoObj.Extension == ".meta")
{
return;
}
// 得到 AB 包名称
strABName = string.Empty;
// 获取资源文件的相对路径
int tmpIndex = fileInfoObj.FullName.IndexOf("Assets");
strAssetFilePath = fileInfoObj.FullName.Substring(tmpIndex); // 得到文件相对路径
// 给资源文件移除 AB 名称
AssetImporter tmpImportObj = AssetImporter.GetAtPath(strAssetFilePath);
tmpImportObj.assetBundleName = strABName;
}
}
}
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
}