Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用
下面代码列出了对于assetbundle资源的常用操作,其中有针对bundle、asset、gameobject三种类型对象的操作,实际使用中尽量保证成对使用。
这一块的操作比较繁琐,但只要使用正确,是可以保证资源完全没有泄露的。
复制代码
using UnityEngine;
using System.Collections;
public class TestAssetBundle : MonoBehaviour
{
public string AssetBundleName = “cube1.assetbundle”;
private string dir = "";
private AssetBundle bundle = null;
private UnityEngine.Object asset = null;
private GameObject go = null;
private void Start() { dir = Application.dataPath + "/StreamingAssets/"; }
private void OnGUI()
{
// 这些操作一遍走下来,申请的资源是可以完全回收的
if (GUILayout.Button("LoadAssetBundle", GUILayout.Width(200), GUILayout.Height(50))) { LoadBundle(); }
if (GUILayout.Button("LoadAsset", GUILayout.Width(200), GUILayout.Height(50))) { LoadAsset(); }
if (GUILayout.Button("Instantiate", GUILayout.Width(200), GUILayout.Height(50))) { Instantiate(); }
if (GUILayout.Button("Destroy", GUILayout.Width(200), GUILayout.Height(50))) { Destroy(); }
if (GUILayout.Button("Unload", GUILayout.Width(200), GUILayout.Height(50))) { Unload(); }
if (GUILayout.Button("UnloadForce", GUILayout.Width(200), GUILayout.Height(50))) { UnloadForce(); }
if (GUILayout.Button("UnloadUnusedAssets", GUILayout.Width(200), GUILayout.Height(50))) { UnloadUnusedAssets(); }
}
// 加载AssetBundle
private void LoadBundle()
{
bundle = AssetBundle.LoadFromFile(System.IO.Path.Combine(dir, AssetBundleName));
if (bundle == null) Debug.LogError("LoadBundle Failed");
}
// 从AssetBundle加载Asset
private void LoadAsset()
{
if (bundle == null) return;
asset = bundle.LoadAsset("Cube1");
if (asset == null) Debug.LogError("LoadAsset Failed");
}
// 根据Asset实例化GameObject
private void Instantiate()
{
if (asset == null) return;
go = GameObject.Instantiate(asset) as GameObject;
if (go == null) Debug.LogError("Instantiate Failed");
}
// 销毁GameObject
private void Destroy()
{
if (go == null) return;
GameObject.Destroy(go);
go = null;
}
// 弱卸载,释放AssetBundle本身的内存
private void Unload()
{
if (bundle == null) return;
// unload完,bundle就不能再用了,记得要置空
bundle.Unload(false);
asset = null;
bundle = null;
}
// 强卸载(无视引用的卸载),释放AssetBundle本身的内存,同时回收从AssetBundle抽取的Asset
private void UnloadForce()
{
if (bundle == null) return;
// unload完,bundle就不能再用了,记得要置空
bundle.Unload(true);
asset = null;
bundle = null;
}
// 全局弱卸载,回收无引用Asset
private void UnloadUnusedAssets()
{
Resources.UnloadUnusedAssets();
}
}
复制代码
由于bundle相互之前会有依赖,所以在使用一个bundle包之前,需要先加载其依赖的所有bundle包,这个过程需要借助全局manifest来实现,简单代码如下:
复制代码
using UnityEngine;
using System.Collections;
public class TestManifest : MonoBehaviour
{
private string dir = “”;
private GameObject go = null;
private AssetBundleManifest manifest = null;
private void Start() { dir = Application.dataPath + "/StreamingAssets/"; }
private void OnGUI()
{
if (GUILayout.Button("LoadAssetBundleManifest", GUILayout.Width(200), GUILayout.Height(50))) { LoadAssetBundleManifest(); }
if (GUILayout.Button("LoadBundle", GUILayout.Width(200), GUILayout.Height(50))) { LoadBundle(); }
}
// 加载Manifest
private void LoadAssetBundleManifest()
{
var bundle = AssetBundle.LoadFromFile(System.IO.Path.Combine(dir, "StreamingAssets"));
manifest = bundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
// 压缩包释放掉
bundle.Unload(false);
bundle = null;
}
// 加载AssetBundle
private void LoadBundle()
{
string bundleName = "assets.resources.test.cube1.prefab.assetbundle";
// 注意:GetAllDependencies会返回直接和间接关联的AssetBundle
// 加载依赖包没有顺序要求
string[] dependence = manifest.GetAllDependencies(bundleName);
for (int i = 0; i < dependence.Length; ++i )
{
AssetBundle.LoadFromFile(System.IO.Path.Combine(dir, dependence[i]));
// 注意:这里不需要手动LoadAsset
// 只需要加载AssetBundle即可
// Asset会在加载其它关联Asset时自动加载
}
var bundle = AssetBundle.LoadFromFile(System.IO.Path.Combine(dir, bundleName));
var asset = bundle.LoadAsset("Cube1");
go = GameObject.Instantiate(asset) as GameObject;
}
}
复制代码