AssetBundleManifest

作者:追风剑情 发布于:2018-11-24 0:23 分类:Unity3d

一、随便创建几个资源并设置AssetBundle名称和Variant名称

1111.png

一、写个生成AssetBundles的菜单功能

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

public class AssetsToolEditor : Editor
{
    [MenuItem("Assets/BuildAssetBundles", false, 100)]
	static void BuildAssetBundles()
    {
        string outputPath = string.Format("{0}/res", Application.streamingAssetsPath);
        Debug.Log(outputPath);
        if (!Directory.Exists(outputPath))
            Directory.CreateDirectory(outputPath);
        BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
        
    }
}


二、测试加载manifest文件

输出的AssetBundles文件, res就是全部资源清单(manifest)

4444.png

示例:加载并解析res

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

public class Test : MonoBehaviour {

	void Start () {
		StartCoroutine("LoadAssetBundleManifest");
	}

	IEnumerator LoadAssetBundleManifest()
	{
		yield return null;
		//加载输出目录下面那个不带后缀的文件
		string path = string.Format("file://{0}/res/res", Application.streamingAssetsPath);
		WWW www = WWW.LoadFromCacheOrDownload(path, 1);
		//WWW www = new WWW(path);
		while (!www.isDone)
            yield return www;
		if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError("加载资源失败 " + path + "\n" + www.error);
        }
        else
        {
            AssetBundle ab = www.assetBundle;
			//提取资源清单
			AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
            //获取全部资源名称
			string[] bundles = manifest.GetAllAssetBundles();
			for(int i=0; i<bundles.Length; i++) {
				Hash128 hash128 = manifest.GetAssetBundleHash(bundles[i]);
				Debug.Log(bundles[i] + " ; hash="+hash128.ToString());
				//获取所有依赖资源(包括间接依赖的资源)
				//string[] dep_bundles = manifest.GetAllDependencies(bundles[i]);
				//仅获取直接依赖的资源(即,不包括间接依赖的资源)
				string[] dep_bundles = manifest.GetDirectDependencies(bundles[i]);
				for(int j=0; j<dep_bundles.Length; j++) {
					Debug.Log("  --"+dep_bundles[j]);
				}
			}

			//获取所有设置了Variant的资源
			Debug.Log("*** GetAllAssetBundlesWithVariant ***");
			bundles = manifest.GetAllAssetBundlesWithVariant();
			for(int i=0; i<bundles.Length; i++) {
				Debug.Log(bundles[i]);
			}

			ab.Unload(false);//释放镜像资源
        }
	}
}


运行时断点查看

全部资源

222.png

依赖资源

5555.png666.png

日志

777.png


标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号