using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestLoadAssetbundle : MonoBehaviour {
//网络地址直接就是url 本地地址需要在前面加 file:// 例如 file://D:\1\cube 1.s (.s是我自定义的assetbundle后缀名,可以自己修改)
[SerializeField]
private string url;
//此处为预设的名字
[SerializeField]
private string assetname;
void Start () {
//在Start方法里开启协程
StartCoroutine(LoadAssetbundleMethod());
}
/// <summary>
/// 加载assetbundle的协程
/// </summary>
/// <returns></returns>
IEnumerator LoadAssetbundleMethod()
{
using (WWW www = new WWW(url)) //using释放资源
{
yield return www; //等待www下载完成
if (www.error != null) //判断是否有错
{
Debug.LogError("网络错误");
}
else
{
AssetBundle bundle = www.assetBundle;
Object obj = bundle.LoadAsset(assetname); //根据prefab的名字下载
Instantiate(obj);
bundle.Unload(false); //释放bundle true:加载的全部卸载 false:用过的卸载掉
}
}
}
}
本文仅作个人学习笔记,方便复习使用