unity3d:Assetbundle模拟加载,同步加载,异步加载,依赖包加载,自动标签,AB浏览器,增量打包

本文详细介绍了Unity中AssetBundle的动态打包过程,包括按需打包模型、UI面板和图集,并阐述了如何通过配置自动设置AB标签。此外,还讨论了AB分发器配置、增量打包机制以及MD5信息的写入。在加载方面,文章提到了编辑器模拟加载和异步加载的实现,并解释了如何处理依赖关系。最后,同步加载的实现和LoadFromMemory接口的性能问题也被提及。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AB自动设置标签

文件夹中每个prefab,单独打包成一个assetbundle,使用于模型,单个UI面板
在这里插入图片描述

文件夹内每个文件夹打包成一个assetbundle,适用于图集
在这里插入图片描述

所有AB分发器配置数据在Editor/AssetBundle/Database/AssetPackage中,该目录下存在目录的一个映射结构,其中的每个序列化文件对应一个AB分发器
在这里插入图片描述

在打包前执行 make tag,通过读上面对应配置,自动设置ab标签与名字

        public AssetBundleChecker(AssetBundleCheckerConfig config)
        {
            this.config = config;
            assetsPath = AssetBundleUtility.PackagePathToAssetsPath(config.PackagePath);
            importer = AssetBundleImporter.GetAtPath(assetsPath);
        }

        public void CheckAssetBundleName()
        {
            if (!importer.IsValid)
            {
                return;
            }

            var checkerFilters = config.CheckerFilters;
            if (checkerFilters == null || checkerFilters.Count == 0)
            {
                importer.assetBundleName = assetsPath;
                string[] bufName = importer.assetBundleName.Split('.');
                MenuAssetBundle.m_abNameStr.AppendFormat("public const string {0} = \"{1}\";", bufName[0], bufName[0]);
                MenuAssetBundle.m_abNameStr.AppendLine();
            }

AB打包

采用增量打包方式,只会打包改动的资源
BuildPipeline.BuildAssetBundles(info.outputDirectory, info.options, info.buildTarget);
调用该函数,unity会自动根据资源的标签进行打包,而且是增量打包,

  1. 对于资源没有变更的bundle包,不会触发重新打包;
  2. 资源没变,即使生成目录下的bundle包被删除了,unity也不会重新打包;
  3. 生成目录下的bundle包对应的manifase被删了,会重新打包;
  4. 可以使用BuildAssetBundleOptions.ForceRebuildAssetBundle参数触发强制重新打包。

打包完毕把md5信息写入

FileStream fs = new FileStream(newFilePath, FileMode.CreateNew);
            StreamWriter sw = new StreamWriter(fs);
            for (int i = 0; i < files.Count; i++)
            {
                string file = files[i];
                //if (file.Contains("StreamingAssets")) continue;
                string ext = Path.GetExtension(file);
                if (file.EndsWith(".meta") || file.Contains(".DS_Store")) continue;

                string md5 = NTG.Util.md5file(file);
                string value = file.Replace(m_OutputPath + "/", string.Empty);

                FileInfo fileInfo = new FileInfo(file);
                int size = (int)(fileInfo.Length / 1024) + 1;

                //if (value != "StreamingAssets" && value != "StreamingAssets.manifest")
                sw.WriteLine(value + "|" + md5 + "|" + size);
            }
            sw.Close(); fs.Close();

AB包浏览器

方便查看一个ab包内具体包含哪些
在这里插入图片描述

黄色的是代表被多个ab包包含的资源

AB异步加载

编辑器下绕过打包模拟加载

if (SimulateAssetBundleInEditor)
			{
				string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
				if (assetPaths.Length == 0)
				{
					Debug.LogError("There is no asset with name \"" + assetName + "\" in " + assetBundleName);
					return null;
				}

                // @TODO: Now we only get the main object from the first asset. Should consider type also.
                UnityEngine.Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[0]);
				operation = new AssetBundleLoadAssetOperationSimulation (target);
			}

异步加载

static public AssetBundleLoadAssetOperation LoadAssetAsync (string assetBundleName, string assetName, System.Type type,string path = "")
		{
			Log(LogType.Info, "Loading " + assetName + " from " + assetBundleName + " bundle");
	
			AssetBundleLoadAssetOperation operation = null;
            {
                assetBundleName = RemapVariantName (assetBundleName);
				LoadAssetBundle (assetBundleName,false,path);
				operation = new AssetBundleLoadAssetOperationFull (assetBundleName, assetName, type);
	
				m_InProgressOperations.Add (operation);
			}
	
			return operation;
		}

加载依赖项:

  1. 加载a生成一个AssetBundleLoadOperation,为异步操作加载
public abstract class AssetBundleLoadOperation : IEnumerator
	{
		public object Current
		{
			get
			{
				return null;
			}
		}
		public bool MoveNext()
		{
			return !IsDone();
		}
		
		public void Reset()
		{
		}
		
		abstract public bool Update ();
		
		abstract public bool IsDone ();
	}
  1. 找到a所有依赖ab包(例如b,c)
string[] dependencies = m_AssetBundleManifest.GetAllDependencies(assetBundleName);
			if (dependencies.Length == 0)
				return;
				
			for (int i=0;i<dependencies.Length;i++)
				dependencies[i] = RemapVariantName (dependencies[i]);
				
			// Record and load all dependencies.
			m_Dependencies.Add(assetBundleName, dependencies);
			for (int i=0;i<dependencies.Length;i++)
				LoadAssetBundleInternal(dependencies[i], false,path);
  1. 把b,c加入到m_DownloadingWWWs中,用www加载
  2. AssetBundleManager中update判断m_DownloadingWWWs每加载完一项,放入到m_LoadedAssetBundles已加载完ab表中
  3. 在AssetBundleManager的Update中遍历m_InProgressOperations中每一项AssetBundleLoadOperation,在a的AssetBundleLoadOperation的update中判断它的依赖b,c是否全部加载完(在m_LoadedAssetBundles中找到值),依赖全加载完,执行加载a的自身ab的请求m_Request = bundle.m_AssetBundle.LoadAssetAsync (m_AssetName, m_Type);
  4. b,c先加载完,a再加载完,AssetBundleLoadOperation中MoveNext返回false,代表执行完毕,可以根据ab包实例化gameobjec之类

同步加载

static private AssetBundle LoadAssetBundleSync(string abname, string abPath = "")
        {

            AssetBundle bundle = null;
            if (!m_LoadedAssetBundles.ContainsKey(abname))
            {
                //byte[] stream = null;
                string uri;
                if (abPath != "")
                {
                    uri = abPath + "/" + abname;
                }
                else
                {
                    uri = AppConst.AbDataPath + "/" + abname;
                }
                //Debug.Log("Loading AssetBundle: " + uri);

                if (!File.Exists(uri))
                {
                    Debug.LogError(String.Format("AssetBundle {0} Not Found", uri));
                    return null;
                }

                //stream = File.ReadAllBytes(uri);

                bundle = AssetBundle.LoadFromFile(uri);
                //stream = null;
                LoadedAssetBundle loBundle = new LoadedAssetBundle(bundle);
          
                m_LoadedAssetBundles.Add(abname, loBundle);

                if (m_Dependencies != null && m_Dependencies.ContainsKey(abname))
                {
                    for (int i = 0; i < m_Dependencies[abname].Length; i++)
                    {
                        LoadAssetBundleSync(m_Dependencies[abname][i]);
                    }
                }
            }
            else
            {
                LoadedAssetBundle loBundle = null;
                m_LoadedAssetBundles.TryGetValue(abname, out loBundle);
                bundle = loBundle.m_AssetBundle;
            }
            return bundle;
        }

关于AssetBundle.LoadFromMemroy内存翻倍问题

LoadFromMemroy即使在PC平台也不如LoadFromFile接口,经测试,PC上LoadFromMemroy接口内存的占用大概会高1/5左右,加载时间比LoadFromFile接口慢1/5左右,而且如loy_liu所说的,LoadFromMemroy接口需要先读取byte[]数组,会导致mono内存的分配,而LoadFromFile不会。
在android平台上,内存的对比将会非常夸张,我这边测试的数据是翻了接近3倍。
所以千万别用LoadFromMemroy接口,LoadFromStream接口没有去测试它的性能和内存,据说和LoadFromFile差不多。
https://answer.uwa4d.com/question/5e8ed8c6cd6a9b49fb4a46a3

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

四夕立羽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值