"Reprint" The ingenious usage of the shared material of the Unity3d Institute (sharedmaterial efficiency issue)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.