当前位置:网站首页>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文件是给我们人看的。
边栏推荐
- What is online account opening? Is it safe to open an account online now?
- flink cep 跳过策略 AfterMatchSkipStrategy.skipPastLastEvent() 匹配过的不再匹配 碧坑指南
- 4 methods for exception handling
- 当面试官让你用两种方式写BinarySort
- 优秀笔记软件盘点:好看且强大的可视化笔记软件、知识图谱工具Heptabase、氢图、Walling、Reflect、InfraNodus、TiddlyWiki
- Two interview demo
- JDBC connection database (MySQL) steps
- P2394 yyy loves Chemistry I
- Use of Jasper soft studio report tool and solution of thorny problems
- DolphinScheduler使用系统时间
猜你喜欢
DBeaver安装与使用教程(超详细安装与使用教程)
理解IO模型
Ingersoll Rand panel maintenance IR Ingersoll Rand microcomputer controller maintenance xe-145m
学习阿里如何进行数据指标体系的治理
PMP考试重点总结五——执行过程组
Bron filter Course Research Report
Data mining modeling practice
基于宽表的数据建模
new URL(“www.jjj.com“)
Use of Jasper soft studio report tool and solution of thorny problems
随机推荐
P2394 yyy loves Chemistry I
The digital human industry is about to break out. What is the market pattern?
Is it safe for Huatai Securities to open an account online? What is the handling process
1181: integer parity sort
1182: effets de la photo de groupe
当面试官让你用两种方式写BinarySort
Resource scheduling and task scheduling of spark
File operations in QT
When the interviewer asks you to write binarysort in two ways
Dbeaver连接人大金仓KingbaseES V8(超详细图文教程)
异常
Calcul des frais d'achat et de vente d'actions
大纲笔记软件 Workflowy 综合评测:优点、缺点和评价
How to reduce the risk of project communication?
Basic knowledge of hard disk (head, track, sector, cylinder)
异常处理4种方法
SQL 優化經曆:從 30248秒到 0.001秒的經曆
new URL(“www.jjj.com“)
From knowledge to wisdom: how far will the knowledge map go?
Why does select * lead to low query efficiency?