1,AssetBundle.LoadFromMemoryAsync
2,AssetBundle.LoadFromFile
3,WWW.LoadFromCacheOrDownload
4,UnityWebRequest
加载方式
一般
T objectFromBundle = bundleObject.LoadAsset(assetName);
GameObject
GameObject gameObject =
loadedAssetBundle.LoadAsset(assetName);
所有资源
Unity.Object[] objectArray =
loadedAssetBundle.LoadAllAssets();
打包
添加标签
物体:
材质:
界面
然后直接build
结果:
加载方式
1
private void Start()
{
//本地加载
AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/StandaloneWindows/box.assetbundles");
GameObject GO = ab.LoadAsset<GameObject>("Cube");
Instantiate (GO);
//加载依赖
AssetBundle abc = AssetBundle.LoadFromFile("AssetBundles/StandaloneWindows/StandaloneWindows");
AssetBundleManifest manifest = abc.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] dependstr = manifest.GetAllDependencies("box.assetbundles");
foreach (string name in dependstr)
{
AssetBundle.LoadFromFile("Assetbundles/StandaloneWindows/" + name);
}
}
所谓的依赖是指物体上需要其它其它包的内容。例如上面的方块中的材质就必须从另一个包获取。
你可以打开记事本观看里面的依赖
2.
IEnumerator Start()
{
//异地内存加载
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("AssetBundles/StandaloneWindows/box.assetbundles"));
yield return request;
AssetBundle ab = request.assetBundle;
GameObject GO = ab.LoadAsset<GameObject>("Cube");
Instantiate(GO);
AssetBundle abc = AssetBundle.LoadFromFile("AssetBundles/StandaloneWindows/StandaloneWindows");
AssetBundleManifest manifest = abc.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] dependstr = manifest.GetAllDependencies("box.assetbundles");
foreach (string name in dependstr)
{
AssetBundle.LoadFromFile("Assetbundles/StandaloneWindows/" + name);
}
}
IEnumerator Start(){
//异地网络加载
while(Caching.ready ==false ){
yield return null;
}
//本地
WWW ww=WWW.LoadFromCacheOrDownload(@"file:///D:\Game\AssertBuilder\AssetBundles\StandaloneWindows\box.assetbundles", 1);
yield return ww;
if (!string.IsNullOrEmpty (ww.error)) {
yield break;
}
AssetBundle ab = ww.assetBundle;
GameObject GO = ab.LoadAsset<GameObject>("Cube");
Instantiate (GO);
}
部署一下服务器
使用的是NetBox2
将打包的文件放在同级目录下
使用网络加载
IEnumerator Start(){
//异地网络加载
while(Caching.ready ==false ){
yield return null;
}
//服务器
WWW ww=WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/StandaloneWindows/box.assetbundles", 1);
yield return ww;
if (!string.IsNullOrEmpty (ww.error)) {
yield break;
}
AssetBundle ab = ww.assetBundle;
GameObject GO = ab.LoadAsset<GameObject>("Cube");
Instantiate (GO);
}
IEnumerator Start()
{
while(Caching.ready ==false ){
yield return null;
}
string uri = @"file:///D:\Game\AssertBuilder\AssetBundles\StandaloneWindows\box.assetbundles";
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);
yield return request.SendWebRequest();
//AssetBundle ab = DownloadHandlerAssetBundle.GetContent (request );
AssetBundle ab=(request .downloadHandler as DownloadHandlerAssetBundle).assetBundle ;
GameObject GO = ab.LoadAsset<GameObject>("Cube");
Instantiate (GO);
}