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

What is the resource management of Unity?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how the resource management of Unity is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The Unity best practice makes it clear that resources should be managed using AssetBundle instead of the Resources directory.

However, things are not as good as Unity's official description. Because we can't even implement an easy-to-use, complete resource management solution with AssetBundle.

According to Unity officials, there are generally two options.

Option 1, if your game is of a level nature, you can load all AssetBundle in one level, and then unload all AssetBundle loaded in this level when entering the next level. But this mechanism seems to work only for Mini Game like Angry Birds: D.

Second, if your game is not level-like, then Unity recommends a resource to count AssetBundle references.

If an object (Asset or other AssetBundle) references this AssetBundle, its reference count is increased by 1. 1. If this AssetBundle is loaded for the first time (that is, the reference count is 0 before loading), you also need to recursively add 1 to its dependent reference count.

If the reference count of an AssetBundle is 0, the AssetBundle is released, and its dependency reference count is recursively minus 1. 0.

Unless we play clearance games like Angry Birds, it seems that there is only option two for us to use. And the second scheme is complete at first glance, because it is an implementation of the GC algorithm.

But if you think about it a little bit, you will find that this scheme is only an AssetBundle management solution, a semi-finished product, how to manage the dependence between resources, Unity has not lost a word, it seems to let users find their own way, which does not seem to be in line with its purpose of easy to learn and use.

Let's analyze the relationship between resources in Unity.

Resources in Unity are roughly divided into the following categories: texture (Texture), mesh (Mesh), animation clip (AnimationClip), audio clip (AudioClip), material (Material), shader (Shader), font resource (Font), and text resource (TextAsset).

There is also a very special existence in AssetBundle, that is, Prefab. AssetBundle.LoadAsset returns GameObject, but it must go through Instantitate and become another GameObject before it can be used. The GameObject mentioned hereafter is the GameObject after Instantitate.

GameObject can add a variety of Component to reference the above resources, and you can also dynamically add or subtract the Component on a GameObject or modify the Component reference to the resource through the code. This flexibility brings great trouble to resource management, and without this flexibility, the implementation of logic will be more troublesome.

Here's an example of how difficult it is to properly manage the reference relationship between GameObject and resources.

Prefab P can generate four GameObject through Instantitate.

After the following code is executed, A references {PMagneT1}, B references {PrecoverT1}, and C references {PGrainT3}. And T2 should be Unload.

1: A.GetComponent () .sprite = (Sprite) T1

2: B.GetComponent () .sprite = (Sprite) T1

3: C.GetComponent () .sprite = (Sprite) T2

4: C.GetComponent () .sprite = (Sprite) T3

In order to automatically and correctly manage the reference relationship between GameObject and resources, you must be aware of the assignment to GameObject.

For example, all sprite assignments must use interfaces like SpriteAssign (SpriteRender sr, Sprite s).

The execution process of SpriteAssign usually looks like this.

Check whether the value of sprite is the same T1. If it is the same, no processing will be done.

Check whether the value of sprite is clone from P, and if not, subtract the reference count of this sprite by 1.

Increase the reference count of T1 by 1

If P is a tree state structure, there is P-(child)-> p1-(child)-> p2.

1: A.p1.p2.GetComponent () .sprite = (Sprite) T1

2: B.p1.p2.GetComponent () .sprite = (Sprite) T1

3: C.p1.p2.GetComponent () .sprite = (Sprite) T2

4: C.p1.p2.GetComponent () .sprite = (Sprite) T3

Step 2 in the SpriteAssign interface is particularly complex, and it must correct the reference relationship as follows: a reference {PMagne T1}, B reference {PMagne T1}, C reference {PMagee T3}.

At the same time, the Destory operation should also be aware, if Destory (A), the resource referenced by A needs to be released, while if Destory (A.p1.p2), the reference of A to the resource needs to be corrected. Because the reference relationship at this time is, A reference {P}. In other words, Destroy will also be more expensive.

Neither assignment nor Destory can be regarded as low frequency operation, especially assignment operation. This overhead is enough to make the program several times slower. Fully automated resource management is impossible if you can't afford these overhead.

I think this is the fundamental reason why Unity does not provide a standard fully automated resource management solution by default.

Inspired by scenario 1, I think we can make a semi-automated resource manager through the following interface.

Void level.open ()

Void level.close ()

Void level.dispose ()

Void stack.push () {

Level l = new level ()

L.open ()

Push l in to stack

}

Void stack.pop () {

Pop l from stack

L.dispose ()

}

Each level object records all loaded resources between level.open () and level.close (), as many times as they are overloaded, and these resources are truthfully released when level.dispose () is executed.

Among them, stack has achieved almost full automation in managing UI resources, calling stack.push when you open a UI, and calling stack.pop when exiting the UI will automatically release all resources you load during this UI.

In other places that do not have the characteristics of stack loading resources, the level class also provides a convenient semi-automatic management scheme.

Most importantly, the overhead and complexity of this scheme is much lower than that of fully automated management.

On how the resource management of Unity is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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