当前位置:网站首页>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文件是给我们人看的。
边栏推荐
- The digital human industry is about to break out. What is the market pattern?
- Explain final, finally, and finalize
- Resource scheduling and task scheduling of spark
- Abnormal occurrence and solution
- Key summary IV of PMP examination - planning process group (2)
- 图解MySQL的binlog、redo log和undo log
- Implementation of single sign on
- Summary of PMP learning experience
- How do I open an account on my mobile phone? Is it safe to open an account online now?
- PMP考试重点总结六——图表整理
猜你喜欢
![1180:分数线划定/P1068 [NOIP2009 普及组] 分数线划定](/img/1a/162b060a6498e58278b6ca50e4953c.png)
1180:分数线划定/P1068 [NOIP2009 普及组] 分数线划定

Illustration of MySQL binlog, redo log and undo log

Learn how Alibaba manages the data indicator system

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

JVM系列(2)——垃圾回收

104. maximum depth of binary tree

new URL(“www.jjj.com“)

线程的生命周期

详解final、finally和finalize

1181:整数奇偶排序
随机推荐
Why does select * lead to low query efficiency?
在本类私有属性直接使用?new()在使用!!!
怎样在手机上开户?现在网上开户安全么?
Methods for creating multithreads ---1 creating subclasses of thread class and multithreading principle
Valentine's Day - VBS learning (sentences, love words)
Custom exception classes and exercises
Two interview demo
This article explains in detail the difficult problems and solutions faced by 3D cameras
new URL(“www.jjj.com“)
Machine virtuelle 14 installer win7 (tutoriel)
Is it safe for Huatai Securities to open an account online? What is the handling process
数据挖掘建模实战
Fastjason filter field
Xiaomi's payment company was fined 120000 yuan, involving the illegal opening of payment accounts, etc.: Lei Jun is the legal representative, and the products include MIUI wallet app
01-分布式系统概述
new URL(“www.jjj.com“)
买卖股票费用计算
Ingersoll Rand panel maintenance IR Ingersoll Rand microcomputer controller maintenance xe-145m
Which securities company is better and safer to choose when opening an account for the inter-bank certificate of deposit fund with mobile phone
Dbeaver连接人大金仓KingbaseES V8(超详细图文教程)