一、AssetBundle静态方法
加载AssetBundle
// path ab包本地路径
// crc(默认0) 未压缩内容的可选 CRC-32 校验和。如果它不为零,则内容将在加载之前与校验和进行比较,如果不匹配则给出错误。
// offset(默认0) 字节偏移量,可以指定从何处开始读取AssetBundle
// 同步加载方法
public static AssetBundle LoadFromFile(string path, uint crc, ulong offset){}
public static AssetBundle LoadFromMemory(byte[] binary, uint crc, ulong offset){}
public static AssetBundle LoadFromStream(System.IO.Stream stream, uint crc, uint managedReadBufferSize){}
// 异步加载方法
public static AssetBundleCreateRequest LoadFromFileAsync(string path, uint crc, ulong offset){}
public static AssetBundleCreateRequest LoadFromMemoryAsync(byte[] binary, uint crc, ulong offset){}
public static AssetBundleCreateRequest LoadFromSteamAsync(System.IO.Stream, uint crc, uint managedReadBufferSize{}
// 从字节数组加载(占内存,慎用)
// LoadFromMemory基本上是无法在手机上用的,因为要多占一份内存,所以大多Unity项目都不进行资源加密。可以使用Unity新添加的API LoadFromStream,在构建AB包时加密,Read时解密
使用举例:
// 同步加载
public void LoadAB(string abName)
{
string path = Path.Combine(Application.streamingAssetsPath, abName);
AssetBundle ab = AssetBundle.LoadFromFile(path);
GameObject obj = ab.LoadAsset<GameObject>("Cube");
GameObject.Instantiate(obj);
}
// 异步加载
public IEnumerator AsyncLoadAB(string abName)
{
yield return new WaitForEndOfFrame();
string path = Path.Combine(Application.streamingAssetsPath, abName);
AssetBundleCreateRequest abcReq = AssetBundle.LoadFormFileAsync(path);
yield return abcReq;
AssetBundle ab = abcReq.assetBundle;
AssetBundleRequest abReq = ab.LoadAssetAsync<GameObject>("Cube");
yield return abReq;
GameObject obj = abReq.asset as GameObject;
GameObject.Instantiate(obj);
}
public void LoadFromStream(string abName)
{
string path = Path.Combine(Application.streamingAssetsPath, abName);
using(FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
{
AssetBundle ab = AssetBundle.LoadFromStream(file);
GameObject obj = ab.LoadAsset<GameObject>("Cube");
GameObject.Instantiate(obj);
}
}
获取、卸载已加载的AssetBundle
public static IEnumerable<AssetBundle> AssetBundle.GetAllLoadedAssetBundles(){}
// unloadAllObjects 是否卸载所有加载的资源,如果为true,会卸载包括正在使用的资源
public static void UnloadAllAssetBundles(bool unloadAllObjects){}
压缩 AssetBundle(Unity 2018.3新增API)
// 可以把资源包重新压缩为Uncompression或LZ4格式,这下资源包的格式完全变得可控了,可以应用更多策略处理资源包。
// inputPath 要再压缩的AssetBundle路径
// outpath 要生成并再压缩的AssetBundle路径,可能与inputPath相同
// method 压缩方法、级别和块大小
// expectedCRC 作为测试依据的AssetBundle CRC
// priority 执行压缩的线程优先级
public static AssetBundleRecompressOperation RecompressAssetBundleAsync(string inputPath, string outputPath, BuildCompression method, uint expectedCRC = 0, ThreadPriority priority = ThreadPriority.Low)
二、AssetBundle实例方法
加载方法
// 同步加载AB包中指定名字(指定类型)的资源
public Object LoadAsset(string name){}
public Object LoadAsset(string name, System.Type type){}
public T LoadAsset<T>(string name){}
// 同步加载AB包中所有(指定类型)的资源
public Object[] LoadAllAssets(){}
public Object[] LoadAllAssets(System.Type type){}
public T[] LoadAllAssets<T>(){}
//
public Object[] LoadAssetWithSubAssets(string name){}
public Object[] LoadAssetWithSubAssets(string name, System.Type type){}
public T[] LoadAssetWithSubAssets<T>(string name){}
// 异步加载AB包中指定名字(指定类型)的资源
public AssetBundleRequest LoadAssetAsync(string name){}
public AssetBundleRequest LoadAssetAsync(string name, Type type){}
public AssetBundleRequest LoadAssetAsync<T>(string name){}
// 异步加载AB包中所有(指定类型)的资源
public AssetBundleRequest LoadAllAssetsAsync(){}
public AssetBundleRequest LoadAllAssetsAsync(Type type){}
public AssetBundleRequest LoadAllAssetsAsync<T>(){}
// 异步加载AB中具有名为 name 的子资源
public AssetBundleRequest LoadAssetWithSubAssetsAsync(string name){}
public AssetBundleRequest LoadAssetWithSubAssetsAsync(string name, System.Type type){}
public AssetBundleRequest LoadAssetWithSubAssetsAsync<T>(string name){}
卸载方法
// 卸载AB包,unloadAllLoadedObjects 如果是true则会立马卸载掉AB包实例化出来的资源,包括正在使用的资源
public void Unload(bool unloadAllLoadedObjects)
其他
// 检测AB包中是否包含某个object
public bool Contains(string name){}
// 获取AB包中所有资产名字(assets/resources/xxx.prefab、assets/resources/yyy.png)
public string[] GetAllAssetNames(){}
// 获取AB包中所有Scene资产路径
public string[] GetAllScenePaths(){}
// 检测AB包是否时流式场景资源
public bool isStreamedSceneAssetBundle{get;}
三、AssetBundle异步AsyncOperation
AssetBundleCreateRequest
// 异步获取创建的AB包,如果直接获取assetBundle会直接变成同步,阻塞线程
public extern AssetBundle assetBundle;
AssetBundleRequest
// 异步获取AB包中的资源,如果直接获取,则会变成同步,阻塞线程
public new Object asset => this.GetResult();
public extern Object[] allAssets;
四、废弃API
// 根据资源路径同步加载AssetBundle,使用新API AssetBundle.LoadFromFile()
public static AssetBundle CreateFromFile(string path){}
// 从内存字节数组同步创建AB包,使用新API LoadFromMemory
public static AssetBundle CreateFromMemoryImmediate(byte[] binary){}
// 从内存字节数组异步加载,使用新API AssetBundle.LoadFromMemoryAsync()
public static AssetBundleCreateRequest CreateFromMemory(byte[] binary)(){}
// Load相关接口全部用新的API LoadAsset 代替
public Object Load(string name){}
// 获取AB包中所有资源的名字,使用新API AssetBundle.GetAllAssetNames()
public string[] AllAssetNames(){}
Comments | NOTHING
该文章已经关闭评论