AssetsBundles加载方式

ab包 加载有两种方式,一种是www 加载 一种是 调用UnityEngine下的Networking库里的

UnityWebRequest 方法;

 下面 是添加一个菜单 按钮   可以把 所有prefabs 打包进 ab包·

using UnityEditor;
public class CreateAssetBundle
{
    [MenuItem("Assets/Build AssetBundles")]

    static void BuildAllAssetsBundles()
    {
        BuildPipeline.BuildAssetBundles("Assets/AssetsBundles",BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows);
    }
}

 

IEnumerator Start()
    {
        url = @"file:///"+Application.dataPath+ "/AssetsBundles/" + "player.assetbundle";
        //www 加载
        using (WWW www = new WWW(url))
        {
            yield return www;
            if (www.error != "" || www.error != null)
            {
                Debug.Log(www.error); 
            }
            else
            {
                AssetBundle ab = www.assetBundle;
                Object pre = ab.LoadAsset(prefabsName);
                Instantiate(pre);
                ab.Unload(false);
            }
        }
        

        //第2种  UnitryWebRequest加载  
        // UnityWebRequest request =  UnityWebRequest.GetAssetBundle(url, 0);
        //yield return request.Send();
        //AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);

        //    Object prefab= bundle.LoadAsset(prefabsName);
        //    Instantiate(prefab);
        //    bundle.Unload(false);


    }

 从 manifest中取得所有 GameObject资源

  void loadAssetBundleManifest()
    {
        AssetBundle assetBundle = AssetBundle.LoadFromFile("Assets/AssetBundle/AssetBundle");
        AssetBundleManifest manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        //string[] depencies = manifest.GetAllDependencies("door.unity3d");

        //foreach (var item in depencies)
        //{
        //    print(item);
        //    AssetBundle shaderBundle =  AssetBundle.LoadFromFile("Assets/AssetBundle/" + item);

        //}

        //加载包中 所有的资源 
        string[] depencies = manifest.GetAllAssetBundles();
        foreach (string item in depencies)
        {
            Debug.Log(item);
            AssetBundle  bundle = AssetBundle.LoadFromFile("Assets/AssetBundle/" + item);
            
            foreach (var obj in bundle.LoadAllAssets(typeof(GameObject))  )
            {

                Instantiate(obj);
            }
        } 

    }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值