AssetBundle 资源加载的几种方式

保存AssetBundle资源
using UnityEditor;
using System.IO;
public class CreateAssetBundles
{
[MenuItem("Assets/Build AssetBundles")]

static void BuildAllAssetBundle()
{
string dir = "AssetBundles";
//  string dir = "D:\AssetBundles";
if (Directory.Exists(dir) == false)
{
Directory.CreateDirectory(dir);
}
BuildPipeline.BuildAssetBundles(dir,BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
} 


加载本地AB资源的几种方式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class LoadFromFileExample : MonoBehaviour
{
IEnumerator Start() 
{

    string path = ("AssetBundles/CubeWall.unity3d");
       AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/share.unity3d");
    //   AssetBundle ab=  AssetBundle.LoadFromFile("AssetBundles/CubeWall.unity3d");

    //   GameObject wallPrefab=  ab.LoadAsset<GameObject>("CubeWall");
    //   Instantiate(wallPrefab);
    //  //Object[] objs=  ab.LoadAllAssets();
    //  // foreach (Object o in objs)
    //  // {
    //  //     Instantiate(o);
    //  // }  


    //第一种加载AB的方式  loadfromMemoryAsync
    //AssetBundleCreateRequest request =  AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));  
    //yield return request;
    //AssetBundle ab = request.assetBundle; 


    // AssetBundle ab=   AssetBundle.LoadFromMemory(File.ReadAllBytes(path));

    //
    //第二种加载AB的方式  loadFromFile; 

    // AssetBundleCreateRequest request= AssetBundle.LoadFromFileAsync(path); 
    //yield return request;
    //AssetBundle ab = request.assetBundle;


    //第三种加载AB的方式  www  
    while (Caching.ready == false)
    {
        yield return null; 
    }
   WWW www=  WWW.LoadFromCacheOrDownload(@"file:///E:\项目\Asset Bundle Project\AssetBundles\CubeWall.unity3d", 1); 
   yield return www;
    if (www.error != null)
    {
        Debug.Log(www.error);
        yield break;
    }

    AssetBundle ab = www.assetBundle;


    //使用
    GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
   Instantiate(wallPrefab);
}

//private void Update()
//{
//    if (Input.GetMouseButtonDown(0))
//    {
//        AssetBundle.LoadFromFile("AssetBundles/share.unity3d");
//    }
//}

}


从服务器下载资源
WWW www=  WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundle Project/AssetBundles/Cubewall.unity3d", 1);


    //第四种方式  UnityWebRqeuest
    // string uri = (@"file:///E:\项目\Asset Bundle Project\AssetBundles\\CubeWall.unity3d"); 

    string uri = @"http://localhost/AssetBundles/Cubewall.unity3d";
     UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);
    yield return request.Send();
    //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request); 

    AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle; 



通过mainfest加载当前资源的所有依赖

    AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
    AssetBundleManifest mainfest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBUndleManifest");

    //foreach (var name in mainfest.GetAllAssetBundles())
    //{
    //    print(name);
    //}

    string[] strs = mainfest.GetAllDependencies("cubewall.unity3d");
    foreach (var name in strs)
    {
        print(name);
        AssetBundle.LoadFromFile("AssetBundles/" + name);
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值