在Unity项目开发过程中,当要做热更新时常常使用一个叫做AssetBundle的东西,这里做一点个人的学习记录

步骤1: 设置打包标签:具体步骤----进入Unity,选择某一资源然后看右下角,在那个地方做这个事情

步骤2: 打对应平台的Bundle包, 这里或多或少要用到一点编辑器扩展的内容,很简单,这里就不介绍编辑器扩展的内容了。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class AssetBundleBuild {

[MenuItem("Assets/BuildAssetBundle/Android")] // 这个打的时安卓包,
    static void BuildAssetBundle(){
        
        Debug.Log ("打包");
        string assetBundleDirectory = "AssetsBundles";
        if (!Directory.Exists (assetBundleDirectory)) {
            Directory.CreateDirectory (assetBundleDirectory);
        }
        string[] file = Directory.GetFiles (assetBundleDirectory);
        for (int i = 0; i < file.Length; i++) {
            Debug.Log (file [i]);
        }
        BuildPipeline.BuildAssetBundles (assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.Android);
        AssetDatabase.Refresh ();
    }

[MenuItem("Assets/BuildAssetBundle/Windows")]
    static void BuildAssetBundleWin(){  // 这个方法执行后资源bundle就会出现在项目汇总AssetsBundles/window 这个目录中
        string path = Application.dataPath + "/AssetsBundles/window";
        if(Directory.Exists(path)){

}else{
            // 创建路径
            Directory.CreateDirectory(path);
        }
        BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.对应平台);
        AssetDatabase.Refresh();
        Debug.Log("win-finish");
    }
}

注: BuildAssetBundleOptions 这个东西叫做资源包编译选项

这里推荐一个地址需要的可以在这里进行学习:https://blog.csdn.net/AnYuanLzh/article/details/81485762

打出来打bundle(以及对应的Manifest文件如图:)

这里提一句:我们所有包的依赖信息都在window里,这里记住一个东西叫依赖

依赖:加入我们有一张图Pic1,在ab1这个bundle里,我们的一个prefab在ab2中,且这个prefab引用了Pic1这个图,

那么,这时就称ab2对ab1有依赖如上图对scene04依赖了sceneo4_mat这个一个assetbundle包那么在其scene04.manifest文件中就会有这么一句:

看图:

知道了这么一个概念我们介绍如何加载资源(我的scene04里面有个Cube,我们这时就要加载这个东西,且,我们Cube引用了scene04_mat的一个材质也就是上图中dependencies存在的原因)

步骤3: 加载资源包

using UnityEngine;
using System.Collections;
public class startLoadbundle : MonoBehaviour
{
    public string manifestAssetBundleName = "Window";//还记得这个window吗? 我们有个window的bundle包,

  // 每一个包的依赖信息就是从这里来的
    public string assetbundleName = "scene04";//想要加载的AssetBundle的名称

private void Awake()
    {
        StartCoroutine(starting());
    }
    IEnumerator starting()
    {
        print(">> startLoadbundle >> Line 9" + assetbundleName); // 这里的asserbundleName是什么都没有的
        string assetbundlePath = "http://localhost/TAssetBundle/window/";/* 要加载的AssetBundle所在的地址(我的在本机的服务器上,其实这个地址不重要,能访问到就行)*/
        string manifestAssetBundlePath = assetbundlePath + "window"; /*因为资源的引用信息都在这个包里,所以我们要先加载这个bundle,这个包的中其实没有资源(这句话是我的个人理解)但是它有我们的bundle,及其依赖的所有信息,只有这个总的bundle里有这些信息,其他地方都没有,我也不知道为啥*/
        WWW wwwmanifestAssetBundle = WWW.LoadFromCacheOrDownload(manifestAssetBundlePath, 0);//下载
        yield return wwwmanifestAssetBundle;//等待下载完成
        if(wwwmanifestAssetBundle.error != null) {
            print(wwwmanifestAssetBundle.error); //  打印出错信息
        }
        AssetBundle manifestAssetBundle = wwwmanifestAssetBundle.assetBundle;//加载包含依赖关系的AssetBundle
        AssetBundleManifest manifest = (AssetBundleManifest)manifestAssetBundle.LoadAsset("AssetBundleManifest");/*从这个bundle中提取出.manifest文件,这样就可以获得所有bundle的依赖信息了,文末贴图 而且其他bundle中*/
        manifestAssetBundle.Unload(false);
        //获取依赖关系列表
        string[] dependAssetBundles = manifest.GetAllDependencies("scene04"); /*获取“scene04”这个bundle 的依赖并返回string[]数组*/
        if(dependAssetBundles.Length != 0){
            for (int i = 0; i < dependAssetBundles.Length; i++)
            {
                print(dependAssetBundles[i]);
            }
        }else{
            print("null");
        }
        AssetBundle[] abs = new AssetBundle[dependAssetBundles.Length];
        for(int i=0;i<dependAssetBundles.Length;i++)
    {
        // 根据刚才的数组加载左右的依赖bundle
            WWW www = WWW.LoadFromCacheOrDownload(assetbundlePath+dependAssetBundles[i],0);
        yield return www;
            abs[i] = www.assetBundle;
            if(www.assetBundle == null){
                print("依赖为空");
            }
    }
        /*注意:以上我们只是加载到了scene04的所有依赖,并没有加载scene04, 依赖加载结束,在这里加载目标bundle*/
        WWW www2 = WWW.LoadFromCacheOrDownload(assetbundlePath + "scene04", 0);
        yield return www2;
        AssetBundle test = www2.assetBundle;
        if(test == null){
            print("Bundle 是空的");
        }
        if(test.LoadAsset("Cube") == null){
            print("Cube" + "是空的");
        }
        object obj = test.LoadAsset("Cube"); /*加载并实例化Cube*/
        GameObject prefabs = (GameObject)obj;
        GameObject cube = GameObject.Instantiate(prefabs);
        test.Unload(false);
    }
}

window.manifest 中的信息,我们能看到这个scene04以及其Dependencies:scene04_mat

Unity3D AssetBundle的打包与加载的更多相关文章

  1. AssetBundle资源打包与加载

    AssetBundle资源打包  1.AssetLabels资源标签 文件名:资源打包成AssetBundle后的文件名,类似于压缩包的名字 后缀:自定义 文件名和后缀名都是小写格式(大写会自动转为小 ...

  2. KEngine:Unity3D资源的打包、加载、调试监控

    资源模块做什么? 资源模块——ResourceModule,是KEngine中最核心的模块,其他模块基本或多或少的对它有依赖,它主要的功能是:资源打包.路径定义.资源管理.资源调试. 资源模块对Uni ...

  3. [Unity] unity5.3 assetbundle打包及加载

    Unity5.3更新了assetbundle的打包和加载api,下面简单介绍使用方法及示例代码. 在Unity中选中一个prefab查看Inspector窗口,有两个位置可以进行assetbundle ...

  4. Unity3d Web3d资源的动态加载

    Unity3d Web3d资源的动态加载 @灰太龙 参考了宣雨松的博客,原文出处http://www.xuanyusong.com/archives/2405,如果涉及到侵权,请通知我! Unity3 ...

  5. 【Unity3D】Unity3D之 Resources.Load 动态加载资源

    [Unity3D]Unity3D之 Resources.Load 动态加载资源 1.Resources.Load:使用这种方式加载资源,首先需要下Asset目录下创建一个名为Resources的文件夹 ...

  6. Unity3d 5.x AssetBundle打包与加载

    1.AssetBundle打包 unity 5.x版本AssetBundle打包,只需要设置好AssetBundle的名称后,unity会自动将其打包,无需处理其他,唯独需要做的是设置好个AssetB ...

  7. Unity5 AssetBundle 打包以及加载

    using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; us ...

  8. 【Unity3d】3d网页游戏场景打包与加载

    http://www.cnblogs.com/dosomething/archive/2012/04/07/2436353.html 3d游戏中  一个场景往往比较大  如果游戏的进行需要下载一个10 ...

  9. Unity3D在移动平台下加载AssetBundle导致Shader效果不正确的问题

    这个问题,主要还是在移动平台下开发导致的. 在编辑器里调试加载AB时会导致Shader效果不正确的原因,主要还是编辑器下加载以IOS或是ANDROID平台打包的AB它所使用的shader已经编译成对应 ...

随机推荐

  1. MinTTY终端模拟器要点

    1.MinTTY是一个Cygwin和MSYS的虚拟终端: 2.支持复制和粘贴操作,支持鼠标操作和右键快捷菜单: 3.支持文本.文件.文件夹的拖放: 4.支持中文,支持UTF-8字符集,支持IME(In ...

  2. Codeforces 741 D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...

  3. 《javascript经典入门》-day02

    <javascript经典入门>-day02 1.使用函数 1.1基本语法 function sayHello() { aler('Hello'); //...其他语句... } #关于函 ...

  4. Lab 11-3

    Analyze the malware found in Lab11-03.exe and Lab11-03.dll. Make sure that both files are in the sam ...

  5. laravel框架安装Curl扩展

    然后进入根目录执行命令     composer update 完毕.

  6. jQuery 让input里面的内容可以布局到页面上

    <div id="selectBox"> </div> <div class="make"> <span class= ...

  7. 567. Permutation in String字符串的排列(效率待提高)

    网址:https://leetcode.com/problems/permutation-in-string/ 参考:https://leetcode.com/problems/permutation ...

  8. ThinkPHP5.0源码学习之缓存Cache(一)

    一.文件 1.缓存配置文件:thinkphp\convention.php 2.缓存文件:thinkphp\library\think\Cache.php 3.驱动目录:thinkphp\librar ...

  9. javascript高级程序设计第3版——第7章 函数表达式

    此张内容的难点在于闭包.而闭包又涉及到原型,原型链,执行上下环境,this的取值等知识点.(此章节对于闭包的内容篇幅较少,且写的很是艰涩难懂,推荐一位大牛的博客,对于闭包的前因后果以及作用机制写的很明 ...

  10. 多线程之interrupt

    1.interrupt()作为中断程序,并不会直接终止运行,而是设置中断状态,由线程自己处理中断.可以选择终止线程.等待新任务或继续执行. 2.interrupt()经常用于中断处于堵塞状态的的线程, ...