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

The difference between material and sharedMaterial in Unity3D Mesh and what is the inference of internal implementation

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

Share

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

This article shows you the difference between material and sharedMaterial in Unity3D Mesh and how the internal inference is realized. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The difference between material and sharedMaterial

Create a Material with the color red

Create two Quad and hang the material you just created. The effect is as follows:

Mount the first Quad to the following script and run:

Render = GetComponent (); render.material.color = Color.white

The effect is as follows:

Modify the script as follows, run:

Render = GetComponent (); render.sharedMaterial.color = Color.white

The color of Quad has changed.

The effect is as follows:

How to explain the above phenomenon, according to the official documents

Renderer.sharedMaterial

SharedMaterial is a shared Material, called a shared material. Modifying a shared material changes the objects used to use that material, and the material settings in the editor change.

Renderer.material

Material is a stand-alone Material that returns the first material assigned to the renderer. Changing the material only changes the material of the object. If the material is used by another renderer, it is cloned and used for the current renderer.

Speculate on the implementation of sharedMaterial and material

Let's guess how material is implemented through some implementations:

We can know from UnityEngine.Renderer that sharedMaterial and material are property. When assigning a value to an attribute, the set method is implicitly called, and the get method is implicitly called when the property is worth it. Suppose the value variable of sharedMaterial is assumed to be _ sharedMaterial, and the value variable of material is assumed to be _ material.

Material _ material; Material _ sharedMaterial;Material origin_sharedMat = render.sharedMaterial;Debug.Log (origin_sharedMat = = render.sharedMaterial); / / True

The get method of sharedMaterial was called. The get of sharedMaterial can be inferred:

Return _ sharedMaterial

one

The second test:

Material new_Mat = new Material (Shader.Find ("Standard")); render.sharedMaterial = new_Mat;Debug.Log (new_Mat = = render.sharedMaterial); / / True

The set method of sharedMaterial was called. The set of sharedMaterial can be inferred:

_ sharedMaterial = value

The third test:

Material origin_sharedMat = render.sharedMaterial;Material origin_Mat = render.material;Debug.Log (origin_sharedMat = = render.sharedMaterial); Debug.Log (origin_Mat = = render.sharedMaterial); Debug.Log (origin_Mat = = render.material); / / Flase True True

After an implicit call to material's get, the sharedMaterial is modified. And _ material and _ sharedMaterial are equal.

Infer the get of material:

If (_ sharedMaterial ~ = _ material) {_ material = new Material (_ sharedMaterial); / / this step is jointly launched by the fourth test _ sharedMaterial = _ material;} return _ material

The fourth test:

Material new_Mat = new Material (Shader.Find ("Standard")); render.sharedMaterial = new_Mat;Debug.Log (new_Mat = = render.sharedMaterial); Material origin_Mat = render.material;Debug.Log (new_Mat = = origin_Mat); Debug.Log (origin_Mat = = render.sharedMaterial); Debug.Log (origin_Mat = = render.material); / / True Flase True True

It is obvious that _ material is not initialized through the material property, because _ sharedMaterial and _ material are not equal before the get method of the material property is called.

Infer the set of sharedMaterial:

_ sharedMaterial = value

The fifth test:

Material new_Mat = new Material (Shader.Find ("Standard")); render.material = new_Mat;Debug.Log (new_Mat.name); Debug.Log (render.sharedMaterial.name); Debug.Log (render.material.name); / / Standard Standard Standard (Instance)

Speculate the set of material:

_ sharedMaterial = value

To sum up, it is inferred that:

Public Material material {get {if (_ sharedMaterial ~ = _ material) {_ material = new Material (_ sharedMaterial); _ sharedMaterial = _ material;} return _ material;} set {_ sharedMaterial = value;}} public Material sharedMaterial {get {return _ sharedMaterial;} set {_ sharedMaterial = value }} memory leaks when using material

Every time Renderer.material is referenced, a new material is generated into memory. When we destroy an object, we need to destroy the material manually, otherwise it will always be in memory.

The official document says:

This function automatically instantiates the materials and makes them unique to this renderer. It is your responsibility to destroy the materials when the game object is being destroyed. Resources.UnloadUnusedAssets also destroys the materials but it is usually only called when loading a new level.

This method automatically instantiates the material and makes it unique to the renderer. When the game object is deleted, you should delete the material manually. You can also delete the material when you call Resources.UnloadUnusedAssets when you replace the scene.

Use material under the editor and sharedMaterial for other platforms

Public static Material GetMaterial (Renderer render) {# if UNITY_EDITOR return render.material; # else return render.sharedMaterial; # endif}

If the protagonist of this kind of gameobject needs to modify the properties of the material or shader attributes, you can use material for the first time, so you can dynamically generate an instance of material, and then use sharedmaterial to dynamically modify the newly generated material, and will not create a new material.

Https://blog.uwa4d.com/archives/optimzation_memory_2.html

In general, the changes of resource attributes are fixed and not random. For example, suppose that when GameObject is attacked, its Material property changes with three different parameter settings depending on the type of attack. So, for this requirement, we recommend that you directly make three different Material, in the case of Runtime, directly replace the Material of the corresponding GameObject through code, rather than change the properties of its Material. In this way, you will find that hundreds of instance Material have disappeared from memory and replaced by these three different Material resources.

The above is the difference between material and sharedMaterial in Unity3D Mesh and what the internal inference is. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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