public class SimpleLoadAssetBundle : MonoBehaviour
{
string path = null;
// Use this for initialization
void Start()
{
path = string.Format("{0}/{1}", Application.streamingAssetsPath, "simpleassetsbundle");
StartCoroutine("GetBundle");
}
private IEnumerator GetBundle()
{
Debug.Log("enter GetBundle");
WWW www = new WWW(path);
yield return www;
Debug.Log("load GetBundle");
if (www.error == null)
{
Debug.Log("success GetBundle");
AssetBundle bundle = www.assetBundle;
GameObject go = GameObject.Instantiate<GameObject>(bundle.LoadAsset("Cube1") as GameObject, this.transform);
go.transform.localScale = Vector3.one;
bundle.Unload(false);
}
else
{
Debug.Log("fail GetBundle");
Debug.Log(">>> error :"+ www.error);
}
www.Dispose();
www = null;
yield break;
}
}
脚本挂载于物体
思路是利用www类来加载ab包
然后读取到相应的包后loadassets指定的ab包中的对象
读取完之后需要注意的是
ab包需要卸载
如果不卸载的话就会占用过多的内存降低性能
以及利用完www之后记得释放置空
注意ab包释放完之后引用有可能也随之置空
所以利用ab包的话需要