当前位置:网站首页>Unity3d:assetbundle simulation loading, synchronous loading, asynchronous loading, dependent package loading, automatic labeling, AB browser, incremental packaging
Unity3d:assetbundle simulation loading, synchronous loading, asynchronous loading, dependent package loading, automatic labeling, AB browser, incremental packaging
2022-07-23 12:53:00 【Sixi Liyu】
AB Automatically set labels
Every... In the folder prefab, Pack them individually into one assetbundle, Used in model , Single UI panel 
Each folder in the folder is packaged into one assetbundle, Applicable to atlas 
all AB The distributor configuration data is Editor/AssetBundle/Database/AssetPackage in , There is a mapping structure of the directory under this directory , Each serialized file corresponds to one AB The dispenser 
Execute before packaging make tag, Read the corresponding configuration above , Automatic setting ab Label and name
public AssetBundleChecker(AssetBundleCheckerConfig config)
{
this.config = config;
assetsPath = AssetBundleUtility.PackagePathToAssetsPath(config.PackagePath);
importer = AssetBundleImporter.GetAtPath(assetsPath);
}
public void CheckAssetBundleName()
{
if (!importer.IsValid)
{
return;
}
var checkerFilters = config.CheckerFilters;
if (checkerFilters == null || checkerFilters.Count == 0)
{
importer.assetBundleName = assetsPath;
string[] bufName = importer.assetBundleName.Split('.');
MenuAssetBundle.m_abNameStr.AppendFormat("public const string {0} = \"{1}\";", bufName[0], bufName[0]);
MenuAssetBundle.m_abNameStr.AppendLine();
}
AB pack
Adopt incremental packaging , Only the changed resources will be packaged
BuildPipeline.BuildAssetBundles(info.outputDirectory, info.options, info.buildTarget);
Call this function ,unity It will be automatically packaged according to the label of the resource , And it is incremental packaging ,
- There is no change to the resource bundle package , Repackaging will not be triggered ;
- Resources have not changed , Even if bundle The package was deleted ,unity Nor will it be repackaged ;
- Generate bundle The package corresponds to manifase Have been deleted , It's going to be repackaged ;
- have access to BuildAssetBundleOptions.ForceRebuildAssetBundle Parameter triggers forced repackaging .
After packing md5 iw
FileStream fs = new FileStream(newFilePath, FileMode.CreateNew);
StreamWriter sw = new StreamWriter(fs);
for (int i = 0; i < files.Count; i++)
{
string file = files[i];
//if (file.Contains("StreamingAssets")) continue;
string ext = Path.GetExtension(file);
if (file.EndsWith(".meta") || file.Contains(".DS_Store")) continue;
string md5 = NTG.Util.md5file(file);
string value = file.Replace(m_OutputPath + "/", string.Empty);
FileInfo fileInfo = new FileInfo(file);
int size = (int)(fileInfo.Length / 1024) + 1;
//if (value != "StreamingAssets" && value != "StreamingAssets.manifest")
sw.WriteLine(value + "|" + md5 + "|" + size);
}
sw.Close(); fs.Close();
AB Package browser
It is convenient to check one ab What does the package contain 
The yellow one represents being multiple ab Resources included in the package
AB Load asynchronously
Bypass the packaging simulation loading under the editor
if (SimulateAssetBundleInEditor)
{
string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
if (assetPaths.Length == 0)
{
Debug.LogError("There is no asset with name \"" + assetName + "\" in " + assetBundleName);
return null;
}
// @TODO: Now we only get the main object from the first asset. Should consider type also.
UnityEngine.Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[0]);
operation = new AssetBundleLoadAssetOperationSimulation (target);
}
Load asynchronously
static public AssetBundleLoadAssetOperation LoadAssetAsync (string assetBundleName, string assetName, System.Type type,string path = "")
{
Log(LogType.Info, "Loading " + assetName + " from " + assetBundleName + " bundle");
AssetBundleLoadAssetOperation operation = null;
{
assetBundleName = RemapVariantName (assetBundleName);
LoadAssetBundle (assetBundleName,false,path);
operation = new AssetBundleLoadAssetOperationFull (assetBundleName, assetName, type);
m_InProgressOperations.Add (operation);
}
return operation;
}
Load dependencies :
- load a Generate a AssetBundleLoadOperation, Load for asynchronous operation
public abstract class AssetBundleLoadOperation : IEnumerator
{
public object Current
{
get
{
return null;
}
}
public bool MoveNext()
{
return !IsDone();
}
public void Reset()
{
}
abstract public bool Update ();
abstract public bool IsDone ();
}
- find a All dependence ab package ( for example b,c)
string[] dependencies = m_AssetBundleManifest.GetAllDependencies(assetBundleName);
if (dependencies.Length == 0)
return;
for (int i=0;i<dependencies.Length;i++)
dependencies[i] = RemapVariantName (dependencies[i]);
// Record and load all dependencies.
m_Dependencies.Add(assetBundleName, dependencies);
for (int i=0;i<dependencies.Length;i++)
LoadAssetBundleInternal(dependencies[i], false,path);
- hold b,c Add to m_DownloadingWWWs in , use www load
- AssetBundleManager in update Judge m_DownloadingWWWs After loading one item , Put in m_LoadedAssetBundles Finished loading ab In the table
- stay AssetBundleManager Of Update Middle traversal m_InProgressOperations Every one of them AssetBundleLoadOperation, stay a Of AssetBundleLoadOperation Of update To judge its dependence b,c Are all loaded ( stay m_LoadedAssetBundles Value found in ), All dependencies are loaded , Execute load a Of itself ab Request m_Request = bundle.m_AssetBundle.LoadAssetAsync (m_AssetName, m_Type);
- b,c Load... First ,a After loading ,AssetBundleLoadOperation in MoveNext return false, The representative has finished execution , According to ab Package instantiation gameobjec And so on
Synchronous loading
static private AssetBundle LoadAssetBundleSync(string abname, string abPath = "")
{
AssetBundle bundle = null;
if (!m_LoadedAssetBundles.ContainsKey(abname))
{
//byte[] stream = null;
string uri;
if (abPath != "")
{
uri = abPath + "/" + abname;
}
else
{
uri = AppConst.AbDataPath + "/" + abname;
}
//Debug.Log("Loading AssetBundle: " + uri);
if (!File.Exists(uri))
{
Debug.LogError(String.Format("AssetBundle {0} Not Found", uri));
return null;
}
//stream = File.ReadAllBytes(uri);
bundle = AssetBundle.LoadFromFile(uri);
//stream = null;
LoadedAssetBundle loBundle = new LoadedAssetBundle(bundle);
m_LoadedAssetBundles.Add(abname, loBundle);
if (m_Dependencies != null && m_Dependencies.ContainsKey(abname))
{
for (int i = 0; i < m_Dependencies[abname].Length; i++)
{
LoadAssetBundleSync(m_Dependencies[abname][i]);
}
}
}
else
{
LoadedAssetBundle loBundle = null;
m_LoadedAssetBundles.TryGetValue(abname, out loBundle);
bundle = loBundle.m_AssetBundle;
}
return bundle;
}
About AssetBundle.LoadFromMemroy Memory doubling problem
LoadFromMemroy Even in PC The platform is not as good as LoadFromFile Interface , After testing ,PC On LoadFromMemroy The memory occupation of the interface is probably high 1/5 about , Loading time ratio LoadFromFile The interface is slow 1/5 about , Moreover, such as loy_liu said ,LoadFromMemroy The interface needs to be read first byte[] Array , It can lead to mono Memory allocation , and LoadFromFile Can't .
stay android On the platform , The comparison of memory will be very exaggerated , My test data is close to 3 times .
So don't use LoadFromMemroy Interface ,LoadFromStream The interface does not test its performance and memory , It's said that and LoadFromFile almost .
https://answer.uwa4d.com/question/5e8ed8c6cd6a9b49fb4a46a3
边栏推荐
- FTP实验及概述
- C: stack stack source code, array stack, chain stack
- MySQL性能优化,索引优化
- 【读书笔记《凤凰架构》- 构架可靠的大型分布式系统.周志明】(一)
- Nas里搭建Frpc客户端【超级无脑】
- Unity3d+GameFramework:资源分析,资源依赖,循环依赖检测
- HCIP-HCIA知识回顾(二)
- Analysis ideas of strong consistency and weak consistency and concurrency skills of distributed scenarios
- PDF Online preview, use of pdf.js
- 简洁描述raft与paxos在设计上的共同点和不同点
猜你喜欢

Learning diary - (routing and switching technology) ACL access control list

C# 自定义Queue队列集合

学习日记——(路由与交换技术)DHCP(动态主机配置协议)

OSPF综合实验

DHCP原理与配置

手动配置DHCP服务

DNS域名解析服务

Learning diary - (routing and switching technology) single arm routing

HCIP --- HDLC和PPP协议

Learning diary - (routing and switching technology) dynamic routing (RIP protocol) and static routing
随机推荐
Unity3d:UGUI源码,Rebuild优化
Analyze redis server
DNS域名解析服务
DHCP second experiment
Unity3d+GameFramework:资源分析,资源依赖,循环依赖检测
Design experience of log file IO system
HCIP---OSPF细节讲解
C# 自定义双向链表
Unity3d:ugui, UI and special effect particle level, bakemesh above 2018.2, particles between two images and in Scrollview
GameFramework:打包资源,打随app发布包,打包生成文件夹说明,上传资源至服务器,下载资源,GameFreamworkList.dat 与GameFrameworkVersion.dat
Learning diary (routing and switching technology) -- floating static routing and default routing
Instant messaging websocket
Understand the article frankly and get the HTTP protocol cache
HCIP-第一次实验
《wireshark网络分析就是这么简单》知识点与技巧
Explain various network protocols in detail
Explain the interactive data flow and block data flow of TCP in detail
Unity3d:ugui source code eventsystem input system FAQ
围棋能力概念与软件开发能力概念的对应
以go语言为例类比侦探推理来讲解【性能分析】