Unity AssetBundle 加载、引用计数实现

 首先,创建一个名为AssetBundleManager的脚本,用于管理 AssetBundle 的打包、加载和引用计数:

using UnityEngine;
using System.Collections.Generic;
using System.IO;

public class AssetBundleManager : MonoBehaviour
{
    private static AssetBundleManager instance;
    private static AssetBundleManifest manifest;
    private static string assetBundleDirectory;

    // 已加载的 AssetBundle 字典
    private static Dictionary<string, AssetBundle> loadedAssetBundles = new Dictionary<string, AssetBundle>();
    // AssetBundle 引用计数字典
    private static Dictionary<string, int> assetBundleRefCount = new Dictionary<string, int>();

    public static AssetBundleManager Instance
    {
        get
        {
            if (instance == null)
            {
                GameObject go = new GameObject("AssetBundleManager");
                instance = go.AddComponent<AssetBundleManager>();
            }
            return instance;
        }
    }

    // 初始化 AssetBundleManager
    public void Initialize(string manifestPath, string assetBundleFolderPath)
    {
        manifest = LoadAssetBundleManifest(manifestPath);
        assetBundleDirectory = assetBundleFolderPath;
    }

    // 加载指定 AssetBundle 中的资源
    public async void LoadAsset<T>(string assetBundleName, string assetName, System.Action<T> callback) where T : UnityEngine.Object
    {
        AssetBundle assetBundle = await LoadAssetBundleAsync(assetBundleName);
        if (assetBundle != null)
        {
            AssetBundleRequest request = assetBundle.LoadAssetAsync<T>(assetName);
            await request;
            T asset = request.asset as T;
            callback?.Invoke(asset);
        }
        else
        {
            callback?.Invoke(null);
        }
    }

    // 卸载 AssetBundle
    public async void UnloadAssetBundle(string assetBundleName)
    {
        if (loadedAssetBundles.ContainsKey(assetBundleName))
        {
            AssetBundle assetBundle = loadedAssetBundles[assetBundleName];
            loadedAssetBundles.Remove(assetBundleName);

            // 引用计数减一
            if (assetBundleRefCount.ContainsKey(assetBundleName))
            {
                assetBundleRefCount[assetBundleName]--;
                if (assetBundleRefCount[assetBundleName] <= 0)
                {
                    assetBundleRefCount.Remove(assetBundleName);

                    // 异步卸载 AssetBundle
                    await assetBundle.UnloadAsync(false);
                }
            }
        }
    }

    // 异步加载 AssetBundle
    private async Task<AssetBundle> LoadAssetBundleAsync(string assetBundleName)
    {
        if (loadedAssetBundles.ContainsKey(assetBundleName))
        {
            // 引用计数加一
            if (!assetBundleRefCount.ContainsKey(assetBundleName))
            {
                assetBundleRefCount.Add(assetBundleName, 1);
            }
            else
            {
                assetBundleRefCount[assetBundleName]++;
            }
            return loadedAssetBundles[assetBundleName];
        }

        string path = Path.Combine(assetBundleDirectory, assetBundleName);
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
        await request;
        AssetBundle assetBundle = request.assetBundle;
        if (assetBundle != null)
        {
            loadedAssetBundles.Add(assetBundleName, assetBundle);
            assetBundleRefCount.Add(assetBundleName, 1);
        }

        return assetBundle;
    }

    // 加载 AssetBundleManifest
    private AssetBundleManifest LoadAssetBundleManifest(string manifestPath)
    {
        AssetBundle manifestAssetBundle = AssetBundle.LoadFromFile(manifestPath);
        return manifestAssetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
    }
}

接下来,创建一个名为BuildEditor的脚本,用于打包指定目录中的资源到 AssetBundle:

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
 
public class BuildEditor : EditorWindow
{
#if UNITY_ANDROID
    static string m_CurPlatformName = "Android";
#elif UNITY_IOS
    static string m_CurPlatformName = "IOS";
#else
    static string m_CurPlatformName = "Windows";
#endif
    /// <summary>
    /// 资源打包路径
    /// </summary>
    private static string m_OutPath = Application.dataPath + "/GameRes";
    /// <summary>
    /// 当前选择的平台
    /// </summary>
    private static BuildTarget m_BuildTarget = EditorUserBuildSettings.activeBuildTarget;
    /// <summary>
    /// 资源输出路径
    /// </summary>
    private static string m_BundlePutPath = Application.streamingAssetsPath + "/" + m_CurPlatformName + "/AssetBundles";
    /// <summary>
    /// 打包列表
    /// </summary>
    private static List<string> finalFiles = new List<string>();
    /// <summary>
    /// 打包完成之后生成的Manifest文件
    /// </summary>
    private static AssetBundleManifest m_Manifest;
    /// <summary>
    /// 打包
    /// </summary>
    [MenuItem("Build/Build ab")]
    private static void Build()
    {
        finalFiles.Clear();
        //1.清除AssetBundleName
        ClearAssetBundleName();
        //2.设置AssetBundleName
        Pack(m_OutPath);
        //3.打包
        if (Directory.Exists(m_BundlePutPath)) Directory.Delete(m_BundlePutPath, true);
        string tempFilePath = m_BundlePutPath;
        Directory.CreateDirectory(m_BundlePutPath);
        m_Manifest = BuildPipeline.BuildAssetBundles(m_BundlePutPath, BuildAssetBundleOptions.DeterministicAssetBundle, m_BuildTarget);
        if (m_Manifest != null)
        {
            DeleteManifestFile(m_BundlePutPath);
            CreateFileList();
            this.BuildSuccessOrFail("Build AB", "Build Succeed", "OK");
        }
        else
        {
            this.BuildSuccessOrFail("Build AB", "Build Fail", "OK");
        }
        EditorUtility.ClearProgressBar();
        AssetDatabase.Refresh();
        Debug.Log("Build Succeed !!!");
    }
    /// <summary>
    /// 清除AssetBundle
    /// </summary>
    public static void ClearAssetBundleName()
    {
        string[] strs = AssetDatabase.GetAllAssetBundleNames();
        foreach (var bundleName in strs)
        {
            AssetDatabase.RemoveAssetBundleName(bundleName, true);
        }
        AssetDatabase.Refresh();
    }
    /// <summary>
    /// 检查文件
    /// </summary>
    /// <param name="path"></param>
    private static void Pack(string path)
    {
        DirectoryInfo infos = new DirectoryInfo(path);
        FileSystemInfo[] files = infos.GetFileSystemInfos();
        for (int i = 0; i < files.Length; i++)
        {
            if (files[i] is DirectoryInfo)
            {
                Pack(files[i].FullName);
            }
            else
            {
                if (!files[i].FullName.EndsWith(".meta"))
                {
                    SetAssetBundleName(files[i].FullName);
                }
            }
        }
    }
 
    /// <summary>
    /// 删除掉当前路径下所有.Manifest文件
    /// </summary>
    /// <param name="path"></param>
    private static void DeleteManifestFile(string path)
    {
        DirectoryInfo infos = new DirectoryInfo(path);
        FileSystemInfo[] files = infos.GetFileSystemInfos();
        for (int i = 0; i < files.Length; i++)
        {
            if (files[i] is DirectoryInfo)
            {
                DeleteManifestFile(files[i].FullName);
            }
            else
            {
                if (files[i].FullName.EndsWith(".manifest"))
                {
                    File.Delete(files[i].FullName);
                }
            }
        }
    }
 
    /// <summary>
    /// 设置AssetBundleName
    /// </summary>
    /// <param name="source"></param>
    private static void SetAssetBundleName(string source)
    {
        string _source = source.Replace(@"\\", "/");
        //截取完整路径到Assets/位置获得Assets路径
        string _assetPath1 = "Assets" + _source.Substring(Application.dataPath.Length);
        //获取Assets/之后的路径 以方便做AssetBunndleName使用
        string _assetPath2 = _source.Substring(Application.dataPath.Length + 1);
        //获取Assets路径下的文件
        AssetImporter assetImporter = AssetImporter.GetAtPath(_assetPath1);
        //获取AssetBundleName
        string bundleName = _assetPath2;
        //获取文件的扩展名并将其替换成.assetbundle
        bundleName = bundleName.Replace(Path.GetExtension(bundleName), ".assetbundle");
        //设置Bundle名
        assetImporter.assetBundleName = bundleName;
        //获取每个文件的指定路径并添加到列表里以方便写file文件使用
        string newFilePath = m_BundlePutPath + PathUtils.GetRelativePath(source, Application.dataPath, "").ToLower();
        string[] strs = newFilePath.Split('.');
        finalFiles.Add(strs[0] + ".assetbundle");
    }
 
    /// <summary>
    /// 生成 file 文件
    /// </summary>
    static void CreateFileList()
    {
        //files文件 目标路径
        string targetFilePath = m_BundlePutPath + "/files.txt";
        //检查是否存在该文件 存在则删除
        if (File.Exists(targetFilePath))
            File.Delete(targetFilePath);
        //统计大小 单位B
        long totalFileSize = 0;
        FileStream fs = new FileStream(targetFilePath, FileMode.CreateNew);
        StreamWriter sw = new StreamWriter(fs);
        int count = 0;
        string file;
        finalFiles.Add(m_BundlePutPath + "/AssetBundles");
        File.Delete(m_BundlePutPath + "/AssetBundles.manifest");
        m_BundlePutPath = m_BundlePutPath.Replace('\\', '/');
        foreach (var files in finalFiles)
        {
            file = PathUtils.NormalizePath(files);
            count++;
            this.UpdateProgress(count, finalFiles.Count + 1, "Createing files.txt");
            //文件Hash
            string hash = "";
            //取到文件路径
            string _path = file.Replace(m_BundlePutPath + "/", string.Empty);
            FileInfo fi = new FileInfo(file);
            if (Path.GetExtension(file) == ".assetbundle")
            {
                //取到文件在Manifest中引用名字
                string abname = file.Replace(m_BundlePutPath + "/", "");
                //通过引用名字去Manifest文件中获取到该文件的Hash值
                hash = m_Manifest.GetAssetBundleHash(abname).ToString();
            }
            else
            {
                hash = this.md5file(file);
            }
            totalFileSize += fi.Length;
            //将文件信息按行写入到files文件中 路径|Hash|文件大小(单位B)
            sw.WriteLine(_path + "|" + hash + "|" + fi.Length);
        }
        //最后写入总大小(单位B)
        sw.WriteLine("" + totalFileSize);
        sw.Close();
        fs.Close();
    }

    /// <summary>
    /// 计算文件的MD5值
    /// </summary>
    public string md5file(string file)
    {
        try
        {
            FileStream fs = new FileStream(file, FileMode.Open);
            System.Security.Cryptography.MD5 md5 = new         
            System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(fs);
            fs.Close();
 
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }
        catch (Exception ex)
        {
            throw new Exception("md5file() fail, error:" + ex.Message);
        }
    }
 
    public void BuildSuccessOrFail(string tittle,string message,string okmsg)
    {
        EditorUtility.DisplayDialog(tittle, message, okmsg);
    }
 
    /// <summary>
    /// 进度条更新
    /// </summary>
    public void UpdateProgress(int progress, int progressMax, string desc)
    {
        string title = "Processing...[" + progress + " - " + progressMax + "]";
        float value = (float)progress / (float)progressMax;
        EditorUtility.DisplayProgressBar(title, desc, value);
    }
}

使用示例 

using UnityEngine;

public class AssetBundleTest : MonoBehaviour
{
    private void Start()
    {
        // 初始化 AssetBundleManager,传入 AssetBundleManifest 路径和 AssetBundle 文件夹路径
        string manifestPath = "路径/manifest";
        string assetBundleFolderPath = "路径/AssetBundles";
        AssetBundleManager.Instance.Initialize(manifestPath, assetBundleFolderPath);

        // 加载 AssetBundle 中的资源
        string assetBundleName = "myassets";
        string assetName = "MyPrefab";
        GameObject myPrefab = AssetBundleManager.Instance.LoadAsset<GameObject>(assetBundleName, assetName);

        if (myPrefab != null)
        {
            // 使用资源
            Instantiate(myPrefab);
        }
        else
        {
            Debug.LogError("Failed to load asset from AssetBundle: " + assetBundleName);
        }

        // 卸载 AssetBundle
        AssetBundleManager.Instance.UnloadAssetBundle(assetBundleName);
    }
}

注解:

  1. 选择要打包成 AssetBundle 的资源文件夹。
  2. 在 Unity 编辑器中,点击菜单栏的Build,然后选择Build ab
  3. AssetBundle 将会被打包到 StreamingAssets 文件夹中。
  4. 在需要加载 AssetBundle 的地方,通过AssetBundleManager.Instance.LoadAsset<T>(assetBundleName, assetName)来加载指定 AssetBundle 中的资源。
    • assetBundleName是 AssetBundle 文件的名称(不带后缀)。
    • assetName是要加载的资源名称。
  5. 加载完毕后,使用资源并在不再需要时调用AssetBundleManager.Instance.UnloadAssetBundle(assetBundleName)来卸载 AssetBundle。

请注意,具体的路径和资源名称应根据你的实际情况进行修改。此外,为了更好地管理资源,你可能还需要额外的代码来处理资源的引用以及资源的释放。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
AssetBundle 引用计数是指在使用 AssetBundle 打包资源的时候,记录每个 AssetBundle 被使用的次数,以便在使用完后及时释放资源,从而避免内存泄漏。在使用 AssetBundle 加载资源时,会对加载的 AssetBundle 进行引用计数的增加,使用完后再进行引用计数的减少,当引用计数为 0 时,就可以释放该 AssetBundle 的资源。 AssetBundle 的打包和加载一般分为以下几个步骤: 1. 打包资源文件:使用 Unity Editor 自带的 AssetBundle 打包工具,将需要打包的资源文件进行打包,生成 AssetBundle 文件。 2. 加载 AssetBundle 文件:在游戏运行时,使用 Unity 提供的 AssetBundle.LoadFromFile 或 AssetBundle.LoadFromMemory 函数来加载 AssetBundle 文件。 3. 加载资源文件:使用加载的 AssetBundle,使用 AssetBundle.LoadAsset 或 AssetBundle.LoadAssetAsync 函数加载需要使用的资源文件。 4. 使用完成后,释放资源:使用 AssetBundle.Unload(false) 函数来释放 AssetBundle 中的资源,同时进行引用计数的减少。如果不再需要该 AssetBundle 中的任何资源,可以使用 AssetBundle.Unload(true) 函数来彻底释放该 AssetBundle,包括清除 AssetBundle 的缓存。 使用 AssetBundle 打包和加载资源可以有效地减少应用程序的内存占用,提高应用程序的性能。同时,在使用 AssetBundle 的时候,需要注意避免重复加载同一个 AssetBundle,以及及时释放不再使用的 AssetBundle 资源,避免内存泄漏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小张不爱写代码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值