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
    评论
### 回答1: Unity 2019.4.x中文文档是指为Unity 2019.4.x版本提供的中文版文档。这份文档是Unity官方提供的开发指南,旨在帮助开发人员使用Unity游戏引擎来创建游戏和应用程序。 Unity 2019.4.x中文文档覆盖了各个方面的内容,包括Unity界面的介绍,项目管理,场景编辑,游戏对象的创建与编辑,材质和纹理的应用,光照和阴影的处理,物理引擎的使用,动画的创建与控制,音频的处理,以及用户界面的设计等等。 这份文档提供了详细的说明和示例,以帮助开发人员了解和掌握各个功能和工具的使用方法。通过阅读文档,开发人员可以学习如何使用Unity的各种组件和系统来实现自己的游戏或应用程序的需求。 同时,Unity 2019.4.x中文文档也提供了大量的编程接口文档,涵盖了Unity中的各种类和函数的用法和说明。这些接口文档使开发人员能够更好地理解Unity引擎的内部结构和工作原理,以便更加高效和灵活地进行开发工作。 总之,Unity 2019.4.x中文文档是Unity官方提供的重要参考资料,对于想要使用Unity引擎进行游戏和应用程序开发的开发人员来说,是一份不可或缺的指南和学习资料。 ### 回答2: Unity 2019.4.x中提供了全面的中文文档支持,为用户提供了更方便、直观的学习和使用体验。 首先,Unity的中文文档涵盖了各个方面的内容,包括引擎的各个模块、功能的使用、编辑器的操作指南等。无论是初学者还是有一定经验的开发者,都能够在中文文档中找到自己需要的信息,帮助他们更好地了解和使用Unity。 其次,Unity的中文文档以详细的说明和示例来解释每个功能和概念。无论是脚本编程、场景编辑、粒子系统还是动画制作,中文文档中都会提供清晰的步骤和例子,帮助用户理解和掌握各种功能。 另外,Unity的中文文档还会根据官方版本进行及时的更新,保持与最新版本的Unity保持同步。这意味着用户可以始终获得最新的特性和改进的详细解释,帮助他们更好地利用Unity的最新功能进行开发。 最后,Unity的中文文档还提供了丰富的教程和案例,使用户可以通过实际操作来学习。这些教程和案例涵盖了不同类型的游戏和应用开发,供用户参考和借鉴。用户可以通过这些实例来加深对Unity的理解,并且可以根据自己的实际需求进行修改和扩展。 总的来说,Unity 2019.4.x中文文档为用户提供了全面、详细的学习和使用指南,帮助他们更好地掌握Unity的各种功能和技术。这些文档的存在使得Unity成为了一个广受欢迎的开发工具,为用户创造了更好的开发环境。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

烫青菜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值