当前位置:网站首页>Unity AssetBundle资源打包与资源加载
Unity AssetBundle资源打包与资源加载
2022-06-28 09:25:00 【尊龍John lone】
AssetBundle是Unity自带的一种资源打包方式,可以把AssetBundle文件当做加密过的压缩包。
一般会编写一个扩展器工具用于打包AB资源。
AssetBundle资源打包前置条件设置:
除了C#脚本外,我们选中的任何东西都会出现这个AB包设置界面。

点击New...新建一个我们想要的名字(它会自动给你全转为小写格式),输入完名称后记得要回车保存一下;
旁边的是后缀,企业开发中常用的后缀有 unity3d,assetbundle,ab,其他自定义。
我这里定义为pedestal.ab,ULua的打包格式就是写死的assetbundle,请留意。
做完这些前置设置后,不管文件在哪个文件夹,它都会识别到然后打包走。看代码。
要放在文件夹里写个路径就会自动创建文件夹并保存在里面了。

编辑器打包AB资源:
1、在项目根文件夹下创建一个“Editor”目录,用于存放编辑器扩展脚本;
2、引入命名空间“UnityEditor”,且脚本不需要继承 MonoBehaviour;
3、编写一个静态无返回值的方法,用于打包AB资源;
4、在方法的上方添加一个“特性”:[MenuItem("menu/itemName")],这个特性可以在Unity菜单上显示我们编写的方法,menu是菜单的名字,itemName是关于这个方法的名字(可以随便写)。


代码演示:
BuildPipeline.BuildAssetBundles(路径, 选项, 平台);
参数分析:
①BuildAssetBundles:打包所有设置了 AssetLabels 的资源;
②路径:打包出来的 AssetBundle 文件存放的位置;
③选项:设置 AssetBundle 打包过程中的选项,None 表示忽略该选项;
④平台:AssetBundle 是平台之间不兼容的,IOS,Android 是两套资源;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class BuildAssetBundles {
//路径,一般打包在这个StreamAssets文件夹中。
string path = Application.streamingAssetsPath;
[MenuItem("AssetBundle/打包所有的AssetBundle")]
static void BuildAllAssetBundleNoral() //体积最小,常用这个.
{
// 路径, 选项.体积最小, 平台.Windows64x.
BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
Debug.Log("AssetBundle打包完毕");
}
[MenuItem("AssetBundle/打包所有的AssetBundle[不压缩]")]
static void BuildAllAssetBundleCompre() //体积最大,加载速度最快.
{
// 路径, 选项.加载速度最快, 平台.Windows64x.
BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows64);
Debug.Log("AssetBundle打包完毕");
}
[MenuItem("AssetBundle/打包所有的AssetBundle[LZ4]")]
static void BuildAllAssetBundleChunk() //2者之间.
{
// 路径, 选项.2者之间, 平台.Windows64x.
BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
Debug.Log("AssetBundle打包完毕");
}
}

资源加载:
1、加载 AB 资源到内存
AssetBundle ab = AssetBundle.LoadFromFile("AB 包完整路径")
从一个完整的路径位置加载 AB 资源包到内存,返回一个 AssetBundle 对象;
2、从 AB 资源中获取资源
T resName = ab.LoadAsset<T>("游戏资源名称")
通过获取到的 AssetBundle 对象的“加载资源”方法,从 AssetBundle 对象内获取对应的游戏物体资源,并且返回该资源。效果类似于使用 Resources.Load 加载一个资源;
3、实例化资源
和使用 Resources.Load 加载到内存中的资源一样,直接实例化生成该物体。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AssetBundleLoad : MonoBehaviour
{
void Start()
{
//从本地加载 AB 资源到内存.
AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "player/player1.ab");
//从 AB 资源中获取资源.
GameObject player = assetBundle.LoadAsset<GameObject>("Necromancer");
GameObject.Instantiate<GameObject>(player, Vector3.zero, Quaternion.identity);
}
}
总结:其实与Resources方法的不同在于,多了一步先加载到内存中,然后再从AB包中取出我们要用到的资源。
扩展:
当你打包完之后会多出了2个文件


它其实是AssetBundle文件的[目录],里面保存了所有ab资源包的路径地址(包含名字),第一个是给计算机看的,第二个.manifest文件是给我们人看的。
边栏推荐
- Campus honey decoration of APP course design (e-commerce platform)
- Dbeaver connects to kingbasees V8 (ultra detailed graphic tutorial)
- 为什么SELECT * 会导致查询效率低?
- Expérience d'optimisation SQL: de 30248 secondes à 0001 secondes
- 什么是在线开户?现在网上开户安全么?
- 买卖股票费用计算
- 数字人行业爆发在即,市场格局几何?
- Custom exception classes and exercises
- 基于宽表的数据建模
- [share OpenGL tutorial]
猜你喜欢

PMP考试重点总结六——图表整理

File operations in QT

A classic JVM class loaded interview question class singleton{static singleton instance = new singleton(); private singleton() {}

PMP examination key summary VIII - monitoring process group (2)

The concept of "tree structure" perfectly interprets the primary and secondary of things

股票 停牌

Interpretation of new products: realm launched GT neo2 Dragon Ball customized version

Linux下安装redis 、Windows下安装redis(超详细图文教程)

数据挖掘建模实战

PMP Exam key summary VI - chart arrangement
随机推荐
Scenario method and error recommendation method for learning basic content of software testing (2)
P2394 yyy loves Chemistry I
异常处理4种方法
Basic knowledge of hard disk (head, track, sector, cylinder)
Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
Resource scheduling and task scheduling of spark
1181: integer parity sort
File operations in QT
Automatic conversion - interview questions
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
A classic JVM class loaded interview question class singleton{static singleton instance = new singleton(); private singleton() {}
Which occupational groups are suitable for the examination
Methods for creating multithreads ---1 creating subclasses of thread class and multithreading principle
Custom exception classes and exercises
Which securities company is better and safer to choose when opening an account for the inter-bank certificate of deposit fund with mobile phone
Virtual machine 14 installing win7 (Figure tutorial)
全局异常处理器与统一返回结果
Screen settings in the source code of OBS Live Room
Valentine's Day - VBS learning (sentences, love words)
小米旗下支付公司被罚 12 万,涉违规开立支付账户等:雷军为法定代表人,产品包括 MIUI 钱包 App