In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to achieve Unity AssetBundle segmentation and merger, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Introduction
Resources in Unity AssetBundle are very easy to extract, and if you want to prevent simple extraction behavior, you can try to split the files at packaging time and merge them at run time.
Packing
When packaging, the file is divided into multiple files, which can be handled by specified rules as needed. For example, you can use a random file name and divide it into random copies.
Load LoadFromStream
Unity 2017.4 Unity 2018.4 Unity 2019.3 Unity 2020.1 or later adds an API that reads AssetBundle from a stream, so multiple files can be merged into a single file stream for API to use.
Unity-Scripting API: AssetBundle.LoadFromStream
Although this API allows you to customize the AssetBundle loading method, including encryption, multi-file reading, in-memory reading, and so on. But there is a fatal disadvantage: it takes up the number of file openings when it is used, and the operating system has an upper limit on the number of file openings.
5.5.4. IOS file handle overuse
Current versions of Unity are not affected by this issue.
In versions prior to Unity 5.3.2p2, Unity would hold an open file handle to an AssetBundle the entire time that the AssetBundle is loaded. This is not a problem on most platforms. However, iOS limits the number of file handles a process may simultaneously have open to 255. If loading an AssetBundle causes this limit to be exceeded, the loading call will fail with a "Too Many Open File Handles" error.
This was a common problem for projects trying to divide their content across many hundreds or thousands of AssetBundles.
Assets, Resources and AssetBundles-Unity Learn
If you need to test on the real machine, you can try to open the file all the time in Update, and then look at the number of open files displayed in the interface. After reaching a certain number, the process will be forced to end.
Using System.Collections.Generic;using System.IO;using UnityEngine
Public class Test: MonoBehaviour {List _ openFileStreams = new List ()
Void OnGUI () {GUILayout.Label (_ openFileStreams.Count.ToString ());}
Void Update () {_ openFileStreams.Add (File.Create (_ openFileStreams.Count.ToString ());}}
Add Too many open files-UWA questions and answers with LoadFromStreamAsync on Android: help developers find better answers
An available MultiStream implementation is provided in the link below, which is recommended for reference. After testing, the AssetBundle can be loaded correctly in Unity 2018.4.12f1 and the resources in it can be instantiated and displayed in the scene.
Combine Multiple Streams in a Single .NET Framework Stream Object
Suggestion: due to the upper limit of the number of files opened, you can consider using MultiStream loading only for critical resources.
LoadFromFile
If you use LoadFromFile API, you need to merge the files in advance, which can be handled at first startup.
The operation of merging files can be processed in the background thread, and the files can be prioritized. Only the files that need to be used immediately will be given priority, and then enter the game after processing; the rest of the files will continue to be processed in the background.
Verification
You can use size and MD5 validation when merging files.
MD5 validation looks good, but when testing in Unity 5.6.6f2 + Xcode 11, it was found that the Debug version took a long time and was not affected in the Release version.
The actual result is that the total 180+MB of 1800 + files takes more than 150 seconds to verify under Debug, and it is suspected that there is a data race problem in the MD5 code.
Considering that MD5 validation doesn't make much sense, because you can guarantee that the files are correct when you package, you can skip this step and only do size verification.
test
You can use the Instruments tool to test time-consuming, and you can easily see calls that are too time-consuming.
For an overview of how to use it, refer to the Unity article:
Optimizing Shader Info Loading, or Look at Yer Data!-Unity Technologies Blog
Optimize
When merging files, be careful to minimize useless operations, such as:
The merged cache Buffer can be pre-created externally and then passed to the merge method to reduce the number of GC and time.
Increase the cache Buffer size and reduce the number of system calls.
Reduce IO operations, including not limited to checking the existence of directories, creating directories, creating files, and so on.
When using a thread, you need to specify that the thread has the highest priority, so you can add more time slices for the thread.
When using StopWatch processing, note that StopWatch provides wall time, not CPU time, so don't be misled.
Reduce the number of split files, and the number of files directly affects the number of IO operations.
Thank you for reading this article carefully. I hope the article "how to split and merge Unity AssetBundle" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.