当前位置:网站首页>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文件是给我们人看的。
边栏推荐
- Music website design based on harmonyos (portal page)
- PMP Exam key summary VI - chart arrangement
- The private attribute of this class can be used directly? New() in use!!!
- 手机买同业存单基金开户选哪家证券公司比较好,比较安全呢
- Data modeling based on wide table
- Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
- Machine virtuelle 14 installer win7 (tutoriel)
- 在本类私有属性直接使用?new()在使用!!!
- 玩玩sftp上传文件
- Which securities company is better and safer to choose when opening an account for the inter-bank certificate of deposit fund with mobile phone
猜你喜欢

PMP考试重点总结八——监控过程组(2)

HDI的盲孔设计,你注意到这个细节了吗?

Basic knowledge of hard disk (head, track, sector, cylinder)

Calcul des frais d'achat et de vente d'actions

JVM family (2) - garbage collection
Understanding the IO model

Decision table method for basic content learning of software testing (2)

SQL optimization experience: from 30248 seconds to 0.001 seconds

Data modeling based on wide table

Why does select * lead to low query efficiency?
随机推荐
Full link service tracking implementation scheme
Multithreading concurrent parallel threaded process
Boundary value analysis method for learning basic content of software testing (2)
SQL 優化經曆:從 30248秒到 0.001秒的經曆
异常处理4种方法
For the development of short video app, the elder warned me to choose the open source code
股票 停牌
A classic JVM class loaded interview question class singleton{static singleton instance = new singleton(); private singleton() {}
小米旗下支付公司被罚 12 万,涉违规开立支付账户等:雷军为法定代表人,产品包括 MIUI 钱包 App
微信小程序开发日志
When the interviewer asks you to write binarysort in two ways
结巴分词器_分词器原理
File operations in QT
大纲笔记软件 Workflowy 综合评测:优点、缺点和评价
1182: effets de la photo de groupe
线程的生命周期
玩玩sftp上传文件
数据挖掘建模实战
The constructor is never executed immediately after new()!!!!!
Static page of pinyougou mall