AssetBundle 打包 本地加载(一)

一 什么是AssetBundle?
assetbundle 是一个包含平台特定资产的存档文件(模型,纹理 ,预设体,音频剪辑,甚至整个场景),都可以在运行时加载。
优点:可用于可下载内容(DLC),减少初始安装大小,为最终用户的平台优化加载资产,减少运行时的内存压力。

ab包一般是在 服务器 本地 和内存的加载,平台之间是不兼容的,例如:iOS 和 安卓

在unity当中所有的贴图都会被unity在发布过程中进行自动打包成图集,在图集打包过程中为了避免相同的图片或者图集被打包两次,我们要设定相同使用的图片在打包过程中为同一图集。
在这里插入图片描述

可以看一下官方的:
链接: https://github.com/Unity-Technologies/AssetBundles-Browser.
下载完成之后放到项目中去的时候一定放在Assets/Editor

蓝色的框是必须需要更改名字的,ab打包的时候需要这个名字
红色框里边是根据这个这个名称来更新版本(可以添加 也可以不添加)
(截图网址不会去掉,这个拖成预设体之后就可以看到了 TVT)
在这里插入图片描述

二 配置
此脚本一定要放在Editor的文件下边 放在其他的地方会打包不了
在unity菜单上边添加上快速打包AssetBundle的快捷方式,方便进行AssetBundle打包。
(如果没有这个方法 请加上命名空间using UnityEditor;)

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

public class AssetBundleLoad 
{
    [MenuItem("AssetBundle/Building")]
    static void Building()
    {
        //获取文件夹的名称
        string path = "Prefab";
        // 判断文件夹是否存在,不存在则新建
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        //                               打包到这个文件夹下边,不需要特殊的压缩方式    适用于Windows64位               
        BuildPipeline.BuildAssetBundles("Prefab", BuildAssetBundleOptions.None ,BuildTarget.StandaloneWindows64);
    }
    
}

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

public class AssetBundleLoad 
{
    [MenuItem("AssetBundle/Building")]
    static void Building()
    {
        //获取文件夹的名称
        string path = "Prefab";
        // 判断文件夹是否存在,不存在则新建
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        //                               打包到这个文件夹下边,不需要特殊的压缩方式    适用于Windows64位               
        BuildPipeline.BuildAssetBundles("Prefab", BuildAssetBundleOptions.None ,BuildTarget.StandaloneWindows64);
    }
    
}

这个脚本可以挂在新建的空物体GameManger身上,因为是通过streamingAssets的路径来查找的所以ab打包出来之后把打包出来的东西剪切到streamingAssets这个文件下边(当然了你可以通过任何的方式来查找这个东西(Reasource),这个文件只是为了方便),清除资源的话用unload这个方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BuildAssets : MonoBehaviour
{
    string _path;
     void Awake()
    {
        StartCoroutine(LoadGameObject());
    }
    public IEnumerator LoadGameObject()
    {
        //通过解压的方式去加载一下
        AssetBundleCreateRequest _assetBundle = AssetBundle.LoadFromFileAsync
            //              这是自己添加的文件方便填写,后边的是需要加载的物体
            (Application.streamingAssetsPath + "/" + "Prefab/cube.unity3d");
        yield return _assetBundle;
        AssetBundle _asset = _assetBundle.assetBundle;
        //进行实例化
        Instantiate(_asset.LoadAsset<GameObject>("Cube.prefab"));
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BuildAssets : MonoBehaviour
{
    string _path;
     void Awake()
    {
        StartCoroutine(LoadGameObject());
    }
    public IEnumerator LoadGameObject()
    {
        //通过解压的方式去加载一下
        AssetBundleCreateRequest _assetBundle = AssetBundle.LoadFromFileAsync
            //       这是自己添加的文件方便填写,后边的是需要加载的物体
            (Application.streamingAssetsPath + "/" + "Prefab/cube.unity3d");
        yield return _assetBundle;
        AssetBundle _asset = _assetBundle.assetBundle;
        //进行实例化
        Instantiate(_asset.LoadAsset<GameObject>("Cube.prefab"));
    }
}

我这是最简单的asset的打包 因为我的cube没有材质球,所以我没有打包材质球,如果物体当中是有材质球的 所以材质球也是需要进行打包的,不然的话加载进去的物体是没有材质的,原先的预设体就可以删除掉了,运行的时候是可以加载上去的(因为我也不是特别的明白,所以大家一起讨论一下或者是教教我吧(表示头有点大,剩下的还有待研究TVT))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小呆子lazy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值