Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Resource Management in Unity Project

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve resource management in the Unity project, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The resources in a 3D game project are mainly composed of maps, models, actions, sounds, etc. The model designer designs the model, and the scene is composed of models. The character is not only an exquisite model, but also takes all kinds of actions, including walking, running, standby, attack and so on. All kinds of basic resources constitute most of the elements of the game.

There are tens of thousands of materials in my Swordsman World Mobile Tour project, and the main city has a 7000 + model. Each character can have different outfits, hairstyles, weapons, pendants, mounts, etc. Hosting so many resources requires very high memory and performance. An excellent resource management solution can carry more content and meet more needs.

Map resource configuration

Unity provides very generous support for this resource management. Take mapping as an example, Unity supports putting the original map directly into the project without any extra processing. Unity automatically generates the final mapping data based on the map configuration. Different platforms (IOS, Android, PC) support different mapping formats. Finally, it is convenient to generate maps in different formats in the form of configuration files. This is a very good practice, while you can intuitively see the performance of different formats in the development environment.

As you can see in the image above, the map has a lot of configuration properties. Different maps will have different configurations, and the configuration of some properties will have a greater impact on memory and performance. As you can see in the image below, this is now a 512 X 512 RGB map with a memory size of 1m. If you limit the use of 50m of map memory, you can only load 50 maps, which is obviously not enough.

A commonly used technique in 3D computer graphics rendering is called Mipmap. To speed up rendering and reduce graphic aliasing, the map is processed into a file consisting of a series of pre-calculated and optimized images. For the 2D mapping used by UI, we need to turn off Mipmap to avoid loss of precision. At the same time, in the test, we also found that enabling Mipmap did not bring much performance improvement. In the mobile game scenario, our line-of-sight distance is very close, and there is not too much demand in this aspect. At the same time, we can save 33% of the memory by shutting down Mipmap, which is a good improvement. After closing Mipmap, we see that the map has become 0.8m, which should be 768KB.

It's a big promotion, but it's important to note that this is a decision made after testing, and the decision is different in different situations.

Next, let's talk about map compression. ETC is the format accepted by all Android, and the compression quality of this format is poor. But in most cases, it is difficult to see the difference on a screen as small as a mobile phone. Rendering itself is a visual deception, and it will be fine if you are satisfied. ETC 2 has a great improvement in image quality, but it needs to be supported by OPGL ES 3.0 or above, depending on which model is supported. There is not much discussion about the compressed format here, as detailed in the Unity official documentation.

After setting the compression format to ETC, the size of the map is 128KB, which is reduced by 6 times, which is a very big improvement.

Let's talk about the attribute of Max Size, do you really need a post as big as 512X512, and is 256X256 enough? Make such a decision, and then see the effect on the real machine. After the map size is limited to 256, the final map size is 32KB. The total reduction is 32 times, and now we can load 1600 maps, which is believed to be enough to meet the needs of most projects.

Finally, let's talk about the Read/Write Enable property, which, if turned on, doubles the runtime map size and stores an extra copy of the map data in memory. In most cases this is not necessary, so turn this property off by default and turn it on when needed.

In this optimal configuration, we reduced the map size by 64 times, from the first 25 to the last 1600, an amazing optimization. Resource format configuration is the most basic module in the overall resource management, but it is also the most important module, which determines how many resources you can display.

Resource allocation tool

Normally, mapping resources are submitted by art, different kinds of maps have different configuration files, and the configuration format of Ios and Android will also be different. It is difficult to require art to have an in-depth understanding of the allocation of resources, and it is very troublesome to configure some attributes every time. At this point, programs are needed to help do some work.

First of all, according to the previous understanding, we extract some of the property settings.

Public class TextureImportData: ImportData {public TextureAlphaMode AlphaMode = TextureAlphaMode.FormTexture; public TextureImporterType TexType = TextureImporterType.Default; public TextureImporterShape ShapeType = TextureImporterShape.Texture2D; public TextureImporterFormat AndroidFormat = TextureImporterFormat.ETC2_RGB4; public TextureImporterFormat IosFormat = TextureImporterFormat.PVRTC_RGB4; public bool ReadWriteEnable = false; public bool MipmapEnable = false; public int MaxSize =-1;}

You can then apply this configuration to a map

Public static void FormatTexture (string path, TextureImportData data) {TextureImporter tImporter = AssetImporter.GetAtPath (path) as TextureImporter; if (tImporter = = null) return; if (tImporter.textureType! = data.TexType) {tImporter.textureType = data.TexType;} tImporter.isReadable = data.ReadWriteEnable; tImporter.mipmapEnabled = data.MipmapEnable; if (data.MaxSize > 0) {tImporter.maxTextureSize = data.MaxSize;} TextureImporterPlatformSettings settingAndroid = tImporter.GetPlatformTextureSettings (EditorConst.PlatformAndroid) SettingAndroid.overridden = true; settingAndroid.format = data.GetFormatByAlphaMode (data.AndroidFormat, tImporter); settingAndroid.maxTextureSize = tImporter.maxTextureSize; tImporter.SetPlatformTextureSettings (settingAndroid); TextureImporterPlatformSettings settingIos = tImporter.GetPlatformTextureSettings (EditorConst.PlatformIos); settingIos.overridden = true; settingIos.format = data.GetFormatByAlphaMode (data.IosFormat, tImporter); settingIos.maxTextureSize = tImporter.maxTextureSize; tImporter.SetPlatformTextureSettings (settingIos); tImporter.SaveAndReimport ();}

Finally, I made a convention or specification with art, which can set the mapping format in batches according to the catalog and a regular.

Generally speaking, more than a dozen rules can cover all cases, and if there are omissions, just add a rule. Another detail to consider here is what to do if a file is overwritten by multiple rules. Imagine adding some general rules at the beginning, followed by some special rules. And according to a rule to set properties, the following rules will also override the previous rules. So here you only need to introduce the order attribute of a rule configuration, and the order can be modified, based on the rules at the bottom of the order. Then for ease of viewing, you can also record the number and size of maps under the current rules for a more intuitive understanding.

Public class ImportData {public string RootPath = "ArtWorks"; public string FileNameMatch = "*. *"; public int Index =-1; public int TotalCount = 0; public int TotalMemuse = 0; public bool PreBuild = true;}

After designing the data, the last thing is to implement a window interface that is convenient to configure these properties, and at the same time, you can display all the information you need on the window. The window is directly implemented in Unity's API, which is still very convenient.

Finally, I give the resource allocation tool to the technical art (Tech Artist) to use, the art side has the demand to adjust, the direct configuration can, do not need to continue to participate.

Resource Management in the Project

Then consider whether there is a misallocation of resources, whether the allocation of resources is reasonable, what kind of situation are we in at present? A plan is needed to get more information. The above tool has seen that you can count the size of resources mapped, and implement a function to export statistics on resource usage, as well as a list of resources sorted by size. Then save each statistical result, and compare the daily statistical results to observe a resource growth in the project.

I specially selected a list of problematic maps, and I can observe that if there is a problem with the configuration, it is easy to expose in the list.

There are also some ideas that have not been put into practice, such as showing not only the size of the map, but also the compression format of the map. I know that the size of this map is 2m, but I don't know whether it is compressed before 2m or after compression. At the same time, it is inevitable that there are repeated maps in the project, you can consider the program to achieve a function to do repeated mapping comparison and so on.

However, to consider the input costs and benefits, pay attention to the Top10 and daily resource changes have a certain degree of control over the project. Energy can be focused on more critical product development, where product quality is actually guaranteed.

These are all the contents of the article "how to achieve Resource Management in the Unity Project". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report