If you need to modify the color of a model material, or modify some of the properties of a material shader, it is common to get the renderer component of the model and get its material property.
To give a simple example, change the color or change the shader directly.
Material.color = color.red;
Material.shader = Resources.load ("Shader/alpha-diffuse", typeof (Shader)) as shader;
Render can use material or a sharedmaterial two attribute.
Renderer render;
render.material;
render.sharedmaterial;
The two attribute usages are the same, but it is best to use sharedmaterial, which is a shared material, regardless of the properties of the material (such as changing the color or replacing the shader), which will only occupy one copy in memory. But if you are using material, unity will automatically new and material action on each change of attribute. It does not cast memory until Application.loadlevel () or resources.unloadunusedassets (). So material is likely to cause a memory leak, so let's just not use it.
However, if you use render.sharedmaterial directly in the code, you will find that in the editor development mode, running for a while the local. Material files are changed, and if they are all in SVN management, then they will all become red exclamation marks. , indicating that the file has been modified. This is too dangerous, accidentally uploaded what to do. To solve this problem, it can be done in a simple way, depending on the platform each time you get material.
12345678 |
public static Material getmaterial(Renderer render) {#if unity_editorreturn render. Material; #elsereturn render. Sharedmaterial; #endif} |
So you don't have to worry about local file changes.
- This article fixed link: http://www.xuanyusong.com/archives/2530
- Reprint Please specify: Rain pine Momo May 03, 2014 Yu Yussong Momo Program Research Institute published
"Reprint" The ingenious usage of the shared material of the Unity3d Institute (sharedmaterial efficiency issue)