当前位置:网站首页>AssetBundle resource management
AssetBundle resource management
2022-06-23 12:40:00 【First wife ash yuanai】
One 、 Manually name and package
1. Write plugins , stay Asset Create in folder Editor Folder , Create a plug-in script inside
2. The name , Name the file to be packaged AssetBundle name
3. pack , Be careful to add MenuItem characteristic , And the platform name , because AssetBundle No cross platform
using System.IO;
using UnityEditor;// Introduce the namespace of the editor
using UnityEngine;
public class BuildAB : Editor // Inherit the editor class
{
[MenuItem("MyMenu/BuildAB-Windows")]
public static void BuildAssetBundle()
{
BuildPipeline.BuildAssetBundles(Application.dataPath + "/AB",// Package output path
BuildAssetBundleOptions.ChunkBasedCompression,// Compression mode [LZ4]
//BuildAssetBundleOptions.None Default mode [LZMA], High compression ratio , Bao Xiao , Loading is slow , It needs to be decompressed as a whole
//BuildAssetBundleOptions.UncompressedAssetBundle [Uncompressed] Uncompressed , Bao Da , Load fast
//BuildAssetBundleOptions.ChunkBasedCompression [LZ4] Medium compression ratio , You can load a specified resource without loading all
BuildTarget.StandaloneWindows64);
}
}
Generated files :

manifest It's an instruction document , contain CRC Check code , Document version number , Dependencies among documents, etc . The contents are as follows :

Two 、 Automatic packing

using System.IO;
using UnityEditor;// Introduce the namespace of the editor
using UnityEngine;
public class BuildAB : Editor // Inherit the editor class
{
private static string needBuildAssetsPath = Application.dataPath + "/needBulid";
private static string assetBundleOutPutPath_win = Application.dataPath + "/AB";
[MenuItem("MyMenu/BuildAB-Windows")]
public static void BuildAssetBundle()
{
// Empty the name
ClearAssetBundleNames();
// The name
SetAssetBundleNames();
// pack
BuildPipeline.BuildAssetBundles(assetBundleOutPutPath_win,// Package output path
BuildAssetBundleOptions.ChunkBasedCompression,// Compression mode [LZ4]
//BuildAssetBundleOptions.None Default mode [LZMA], High compression ratio , Bao Xiao , Loading is slow , It needs to be decompressed as a whole
//BuildAssetBundleOptions.UncompressedAssetBundle [Uncompressed] Uncompressed , Bao Da , Load fast
//BuildAssetBundleOptions.ChunkBasedCompression [LZ4] Medium compression ratio , You can load a specified resource without loading all
BuildTarget.StandaloneWindows64);
// Empty the name
ClearAssetBundleNames();
// Refresh
AssetDatabase.Refresh();
}
/// <summary>
/// Empty all Bundle name
/// </summary>
private static void ClearAssetBundleNames()
{
var allName = AssetDatabase.GetAllAssetBundleNames();
for (int i = 0; i < allName.Length; i++)
{
AssetDatabase.RemoveAssetBundleName(allName[i], true);// Clear even when in use
}
}
private static void SetAssetBundleNames()
{
// You need to package the root directory of the file
DirectoryInfo di = new DirectoryInfo(needBuildAssetsPath);
// Open Directory
OpenDirectoryAndSetName(di);
// Traverse all subfolders
/*foreach (var item in di.EnumerateDirectories())
{
Debug.Log(item.FullName);
}*/
// Traverse all of meta file
/*foreach (var item in di.EnumerateFiles())
{
Debug.Log(item.FullName);
}*/
// Traverse all files and folders
/*foreach (var item in di.EnumerateFileSystemInfos())
{
Debug.Log(item.FullName);
}*/
}
private static void OpenDirectoryAndSetName(DirectoryInfo dirInfo)
{
foreach (var item in dirInfo.EnumerateFileSystemInfos())
{
if (item is DirectoryInfo)
{
// Recursively open subfolders
OpenDirectoryAndSetName(item as DirectoryInfo);
}
else
{
if (item.Name.EndsWith(".meta"))
continue;
SetFileBundleName(item.FullName);
}
}
}
/// <summary>
/// Start the file Bundle name
/// </summary>
/// <param name="fileFullName"></param>
private static void SetFileBundleName(string fileFullName)
{
// Get relative path
string relativePath = "Assets" + fileFullName.Substring(Application.dataPath.Length);
// Get the file name [ To prefix ]
string fileName = fileFullName.Substring(fileFullName.LastIndexOf('\\') + 1);
// Remove suffix
fileName = fileName.Remove(fileName.LastIndexOf('.'));
// Start the file Bundle name
// Access to resources AssetImporter object
AssetImporter fileImportor = AssetImporter.GetAtPath(relativePath);
fileImportor.assetBundleName = fileName;
}
}
边栏推荐
- 首次曝光!唯一全域最高等级背后的阿里云云原生安全全景图
- halcon原理:一维函数function_1d类型【2】
- mysql innodb的redo log buffer中未commit的事务持久化到redo log后,万一事务rollback了怎么办?
- 二维激光SLAM( 使用Laser Scan Matcher )
- Unity learning day14 -- collaboration and WWW
- ROS察微【51】:如何将里程计和 IMU 与 robots_localization 融合
- What if the test time is not enough?
- 生态 | 万里数据库与卫士通完成兼容认证 共筑网络安全生态体系
- 全新项目,如何保证测试的覆盖率?
- Huawei cloud gaussdb heavily released HTAP for commercial use, defining a new paradigm of cloud native database 2.0
猜你喜欢

With 32 qubits! Rigetti computing enters the UK quantum computing market

Basic data type and corresponding packing class

ROS2知识(6):坐标对象TF原理和实践

二维激光SLAM( 使用Laser Scan Matcher )

Playing in Singapore in the hot summer: an inventory of indoor attractions and good places for night trips

Halcon knowledge: dyn_ Usage of threshold (scratch detection)

Halcon principle: Auto_ Threshold operator

Qt5 knowledge: string list qstringlistmodel

"Developer talk" nail connector +oa approval to realize digitalization of school students' leave and work scenes

全新项目,如何保证测试的覆盖率?
随机推荐
&lt;Sicily&gt;1001. Rails
ROS2知识(1):开始实践机器人
UI框架
解决“Thread 1: “-[*.CollectionNormalCellView isSelected]: unrecognized selector sent to instance 0x7f”
唐人街徒步:在异国情调的纽约感受浓厚的中式气息
With 32 qubits! Rigetti computing enters the UK quantum computing market
Qt5 knowledge: QT drawing graph
Synergetic process
Deveco device tool helps openharmony device development
ROS knowledge: point cloud files PCD format
A bug development means that the user will not operate like this, and there is no need to repair it. How should testers respond?
UI framework
QT knowledge: QT widgets widget class [01]
Ablebits Ultimate Suite for Excel
TT-SLAM:用于平面环境的密集单目SLAM(IEEE 2021)
Deep thinking: in-depth analysis of some scenes in Gaia Altman and the philosophy and perception reflected
QT knowledge: using the qgraphicspixmapitem class
Explain the relationship between pyqt5 signal and slot in detail
Voice module: pyttsx sound change project
Unity学习Day14--协程和WWW