Unity之AssetBundle5.X打包及下载

  • 5.0以后打包更简单,调用BuildPipeline.BuildAssetBundles,引擎将自动根据资源的assetbundleName属性批量打包,自动建立Bundle以及资源之间的依赖关系。
  • 5.0以后的版本中,一些4.x中的旧有策略被默认开启。 CompleteAssets:用于保证资源的完备性,默认开启;CollectDependencies:用于收集资源的依赖项,默认开启;DeterministicAssetBundle:用于为资源维护固定ID,默认开启。
  • 5.0以后版本打包时,无法指定Assetbundle.mainAsset,因此无法再通过mainAsset来直接获取资源。
  • 如果项目中只会运用到.prefab,那么只需要给prefab设置assetbundleName,相关的贴图、材质不必设置,最后加载prefab并实例化后材质贴图仍会存在。但如果需单独运用prefab身上的贴图或材质,则这些贴图材质也需要设置assetbundleName。
以Window平台为例:
--------------------------------------打包------------------------------------------
  • 5.X版本打包时与4.X版本打包时,注意文件的名称不同,5.X:名称为assetbundleName,4.X:名称为xxx.assetbundle。
  • 当AssetBundle标记的对象很多时,想要看到包含某个字符串的AssetBundle(可能有多个)中的资源时,可以单击AssetBundle的名称选项,在弹出的菜单中选择Filter Seleted Name;或者在Project视图搜索“b.AssetBundle名称”。
     --------------------打包方法一:提前设置AssetBundleName----------------------

   public static AssetBundleManifest BuildAssetBundles(string outputPath, BuildAssetBundleOptions         assetBundleOptions, BuildTarget targetPlatform);
    [MenuItem("AssetBundle/AssetBundleWindow5.0Method1")]
    public static void BuildAssetToWindow5_0Method1()
    {
        string outPath = Application.dataPath + "/../BuildAssetBundle/Window/";
        CreateDir(outPath);
        BuildPipeline.BuildAssetBundles(outPath,
            BuildAssetBundleOptions.ForceRebuildAssetBundle, BuildTarget.StandaloneWindows);
        AssetDatabase.Refresh();
    }
    public void CreateDir(string path)
    {
        DirectoryInfo info = new DirectoryInfo(path);
        if (!info.Exists)
        {
            info.Create();
        }
    }
设置assetbundleName:        将打包得到的文件拷贝到StreamingAssets中:
Unity之AssetBundle5.X打包及下载    Unity之AssetBundle5.X打包及下载

   --------------------打包方法二:动态设置AssetBundleName----------------------
 public static AssetBundleManifest BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);
  • 所要打包资源的后缀名要写。
    [MenuItem("AssetBundle/AssetBundleWindow5.0Method2")]
    public static void BuildAssetToWindow5_0Method2()
    {
        string outPath = Application.dataPath + "/../BuildAssetBundle/Window/";
        CreateDir(outPath);
        AssetBundleBuild[] builds = new AssetBundleBuild[1];
        builds[0]. assetBundleName = "moshushi";
        string[] paths = new string[5];
        paths[0] = "Assets/AssetBundle/MoShuShi.prefab";
        paths[1] = "Assets/Model/Materials/fk02_02_moshushi.mat";
        paths[2] = "Assets/Model/Materials/fk02_02_tuzi.mat";
        paths[3] = "Assets/Model/fk02_02_moshushi.jpg";
        paths[4] = "Assets/Model/fk02_02_tuzi.jpg";
        builds[0]. assetNames = paths;
        BuildPipeline.BuildAssetBundles(outPath, builds
            BuildAssetBundleOptions.ForceRebuildAssetBundle, BuildTarget.StandaloneWindows);
        AssetDatabase.Refresh();
    }
       Unity之AssetBundle5.X打包及下载
--------------------------------------加载------------------------------------------
  • 5.X版本打包出来后与4.X版本打包出来后进行下载时,注意下载文件的名称,5.X:名称为assetbundleName,4.X:名称为xxx.assetbundle
using System.Collections;
using System.IO;
using UnityEngine;

public class DownLoadASB : MonoBehaviour
{
    public GameObject Sphere;
    private string ModelPath;//服务器路径
    private string LocalPath;//本地路径
    void Awake()
    {
#if UNITY_EDITOR
        ModelPath = "file:///" + Application.streamingAssetsPath + " /moshushi";
        LocalPath = Application.persistentDataPath + "/Model/ moshushi";
#endif
#if UNITY_ANDROID
        ModelPath = Application.streamingAssetsPath + "/moshushi";
        LocalPath = Application.persistentDataPath + "/Model/moshushi";
#endif
#if UNITY_IPHONE
        ModelPath = "file:///"+Application.streamingAssetsPath + "/moshushi";
        LocalPath = Application.persistentDataPath + "/Model/moshushi";
#endif
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (File.Exists(LocalPath))//本地存在
            {
                print("本地下载" + LocalPath);
                StartCoroutine(DownLoadModel("file:///" + LocalPath));
            }
            else
            {
                print("服务器下载");
                StartCoroutine(DownLoadModel(ModelPath));
            }
        }
    }
    IEnumerator DownLoadModel(string path)
    {
        WWW www = new WWW(path);
        yield return www;
        if (www.error == null)
        {
            byte[] bytes = www.bytes;
            CreateDir(Application.persistentDataPath + "/Model");
            if (!File.Exists(LocalPath))
            {
                File.WriteAllBytes(LocalPath, bytes);
            }
            AssetBundle bundle = www.assetBundle;
--------------------------加载资源方法一:同步加载------------------------------
            GameObject go = bundle.LoadAsset("MoShuShi") as GameObject;
            Instantiate(go, Vector3.zero, Quaternion.identity);
            Material mat = bundle.LoadAsset("fk02_02_moshushi.mat") as Material;
            Sphere.GetComponent().material = mat;
--------------------------加载资源方法二:异步加载------------------------------
            AssetBundleRequest request1 = bundle.LoadAssetAsync("MoShuShi");
            yield return request1;
            if (request1.isDone)
            {
                GameObject Model = request1.asset as GameObject;
                if (Model != null)
                {
                    Instantiate(Model, Vector3.zero, Quaternion.identity);
                }
                else
                {
                    print("资源不存在");
                }
            }
            AssetBundleRequest request2 = bundle.LoadAssetAsync("fk02_02_moshushi");
            yield return request2;
            if (request2.isDone)
            {
                Material newmat = request2.asset as Material;
                if (mat != null)
                {
                    Sphere.GetComponent().material = newmat;
                }
                else
                {
                    print("贴图不存在");
                }
            }
            bundle.Unload(false);
        }
        else
        {
            print(www.error);
        }
    }
    public void CreateDir(string path)
    {
        DirectoryInfo info = new DirectoryInfo(path);
        if (!info.Exists)
        {
            info.Create();
        }
    }
}
本地路径:
Unity之AssetBundle5.X打包及下载

项目运行前:                               项目运行后:
Unity之AssetBundle5.X打包及下载  Unity之AssetBundle5.X打包及下载


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烫青菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值