构建脚本路径:Editor/BuildAssetBundles
先给需要打包的资源指定 AssetBundle 标签
然后点击菜单栏 Assets/Build AssetBundles 构建打包
// 从文件读取
AssetBundle ab = AssetBundle.LoadFromFile(Path);
// 异步(需要协程)
// AssetBundleCreateRequest abRequest = AssetBundle.LoadFromFileAsync(objPath);
// yield return abRequest;
// AssetBundle ab = abRequest.assetBundle;
// 从内存读取
AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(Path));
// 异步同上
// AssetBundle ab = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(Path));
// .....
需要先把打包好的AssetBundle
文件放在webSereve
目录下,然后启动webSereve服务器
。
void Start()
{
// 开启协程
StartCoroutine(GetAssetBundle());
}
IEnumerator GetAssetBundle()
{
string uri = @"http://localhost/AssetBundles/obj.lcl";
UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(uri);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
GameObject prefab = bundle.LoadAsset<GameObject>("assetBundle_Cube");
Instantiate(prefab);
}
}
ab.Unload(false);
ab.Unload(true);
下载解压文件到Editor文件夹即可
点击 window -> AssetBundle Browser 打开查看