Unity AssetBundle 之 (进阶)简单的自动 清理/移除 指定目录下的资源上 AssetBundle 的标签 的方法

 

 

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
}

 

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Unity AssetBundleUnity 引擎中一种用于打包和管理资源的机制。通过使用 AssetBundle,我们可以将资源打包成一个个独立的包,然后在运行时根据需要动态加载和卸载这些资源,从而实现游戏资产的按需加载,节省内存和加载时间。 进阶的实现 AssetBundle 框架包括以下步骤: 1. 资源分类与打包:根据游戏的需求,将资源按照类型进行分类,并使用 Unity 的打包工具将资源打包成 AssetBundle。 2. 资源加载与管理:在游戏运行时,通过 AssetBundle 的加载接口加载需要的资源,可以使用 WWW 或 UnityWebRequest 进行加载,并使用 AssetBundleManifest 来管理加载的 AssetBundle。 3. 资源缓存与卸载:加载并使用资源后,可以将资源缓存在内存中,以便后续快速访问。当资源不再需要时,可以使用 Unload 方法释放资源,以节省内存。 4. 异步加载与加载进度:为了防止资源加载阻塞游戏主线程,可以使用异步加载的方式加载资源,并通过回调函数获取加载进度信息,以便显示加载界面或进度条。 5. 资源更新与热更:在游戏发布后,如果需要更新或替换某些资源,可以使用 AssetBundle 的版本控制机制,根据服务器资源版本信息判断是否需要更新,并进行资源差异化下载和替换。 6. 跨平台适配:Unity AssetBundle 可以在不同平台(如 Windows、iOS、Android)上使用相同的打包和加载方式,因此可以方便地实现跨平台适配。 通过以上步骤,我们可以实现一个简单的 AssetBundle 框架,实现资源的按需加载、缓存、卸载、异步加载和更新等功能。这样可以大幅减少游戏的内存占用和加载时间,提高游戏性能和用户体验。 ### 回答2: Unity assetbundleUnity引擎中用于打包和加载资源的一种方式。在进阶简单实现assetbundle框架中,我们需要完成以下几个步骤: 1.资源打包:首先,我们需要将需要打包的资源准备好,包括场景、模型、纹理、音效等。然后,使用Unity的AssetBundle API对这些资源进行打包。这一步的关键是给资源设置正确的AssetBundleName和Variant。 2.资源加载:在游戏运行时,我们可以通过AssetBundleManager来加载和管理assetbundle资源。AssetBundleManager是一个自定义的管理器类,使用字典来存储已加载的assetbundle,以便快速获取。 3.动态加载:使用AssetBundleManager加载资源后,可以通过AssetBundleManager提供的接口来实例化、替换、销毁已加载的资源。在需要使用资源的地方,使用Instantiate方法来实例化预制体,或者直接使用LoadAsset方法来加载非预制体资源。加载完成后,可以使用UnityEngine.Object类型来获取具体的资源。 4.资源卸载:当资源不再需要使用时,可以调用AssetBundleManager提供的接口来卸载资源。这样,可以释放内存并减少资源的加载数量。需要注意的是,在卸载资源时,需要考虑其他依赖资源的情况,避免出现因卸载资源而导致其他资源无法使用的问题。 5.异常处理:在加载资源的过程中,可能会出现一些异常情况,例如资源不存在、加载失败等。我们需要在框架中做好异常处理的逻辑,在这些情况下给出合适的提示信息,并采取相应的措施,以保证游戏的正常运行。 综上所述,一个简单的assetbundle框架的实现包括资源打包、加载、动态加载、资源卸载以及异常处理等步骤。这样的实现可以提高游戏的性能和加载速度,减少内存占用,并且便于资源的管理和更新。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仙魁XAN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值