AssetBundle打包知识

Assetbundle的用处:
1.assetbundle是一个压缩包,里面包含模型、贴图、预制体
声音、场景等
2.assetbundle保存自身的相互依赖关系
3.assetbundle使用的压缩算法一般用LZMA和LZ4压缩算法,
可以减少包的大小,能更快的进行网络传输。
4.下载的内容可以放在assetbundle里面,可以减少安装包的
大小,在游戏运行需要时进行下载。

Assetbundle的使用流程
1.指定资源的AssetBundle的属性

2.构建AssetBundle包

3.上传AB包

4.加载AB包和包里的资源

1.在unity中创建一个工程,导入一些资源
在这里插入图片描述

然后选择需要打包的资源(我这需要打包这个预制体)
在这里插入图片描述

在这里填写assetbundle的名字和后缀名。(点击assetbundle会弹出一个对话框,点击new,然后填写需要的名字)
在这里插入图片描述

2.然后创建一个脚本CreateAssetBundle.cs

 using UnityEditor;
using System.IO;

public class CreateAssetBundle 
{
    [MenuItem("Assets/Build AssetBundle")]
    static void BuildAllAssetBundle()
    {
        string output = "AssetBundles";
        if (Directory.Exists(output)==false)
        {
            Directory.CreateDirectory(output);
        }
        BuildPipeline.BuildAssetBundles(output, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

然后在菜单栏的Assets菜单下有
在这里插入图片描述

点击这个打包
在项目的根目录下有一个AssetBundle文件夹
在这里插入图片描述在这里插入图片描述

3.利用ab包加载资源
创建脚本LoadAB.cs,将脚本挂在摄像机上

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

public class LoadAB : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        AssetBundle ab= AssetBundle.LoadFromFile("AssetBundles/wall.ab");
        GameObject go= ab.LoadAsset<GameObject>("Cube");
        Instantiate(go);
    }
}

运行后的结果
在这里插入图片描述

已经将AB包中的资源加载出来了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值