Ever wanted to automatically apply changes from one material to multiple others? Or to reuse a Base Material for local tweaks? In Unity 2022.1, we’re introducing a concept similar to Prefabs, compatible with all render pipelines, to manage Material Variants in the Editor.
Made for teams or individual creators managing several materials at once, Material Variants can improve collaboration, facilitate the reusability of materials, speed up iterations, and enhance asset libraries with smooth updates and reduced risk of error. This highly requested solution enables the reuse and refined management of materials that share most surface properties but differ through others.
With Material Variants, you can create templates or Material Prefabs based on regular materials. This results in variants that share common properties with the template material and override the properties that differ.
Changing common and non-overridden properties in the template material is then automatically reflected in the variant materials. You can also lock certain properties in a material to prevent them from being overridden in the variants.
As an example, you can set up the variation hierarchy of wood materials in your project this way:
When changing the smoothness/metallic properties in the Wood material, modifications are automatically reflected in all materials in the hierarchy.
Here are three key benefits of Material Variants:
Material Variants are regular Material Assets that store references to their parents, in addition to the list of overridden parameters. To guarantee strong performance in-Editor, inherited values are cached, and hierarchies are only resolved once a material is accessed and one of its parents has changed since the last resolve.
Unlike Prefabs, Material Variants don’t rely on the Asset Database to maintain hierarchies. When a change occurs in a hierarchy, there is no need to internally reimport the children materials; only children loaded in memory will be updated with newly inherited values, prompting faster iteration.
Please note, however, that the concept of Material Variants is only available in the Editor to improve material authoring workflows. It has no impact at runtime, and does not serve to optimize overall runtime performance. It can be used to author different materials across different platforms, so long as they use the same shader. Since only one shader is allowed per Material Variant hierarchy, you cannot set a specific shader at a given level to target a particular platform. Nonetheless, you can use Material Variants to edit Material properties or enable a specific keyword when targeting a platform.
Material Variants don’t inherit keywords from their parents. This is because the keyword state often depends on the value of other properties. For example, a keyword might only be set when a texture is assigned to the material. To support this workflow, we’ve added a new API to the ShaderGUI class called ValidateMaterial. This method is called every time a material using the ShaderGUI is modified, or when its hierarchy is resolved in the case of a Material Variant. This helps to ensure that the keyword state is up to date.
Finally, if you’re looking to create custom GUI for fields in the Material Inspector, there’s now a pair of functions added, similar to existing functions for Prefabs: BeginProperty and EndProperty. Wrapping custom GUI code for a property between these calls will display lock icons in Unity and context menus for Material Variants.
To use Material Variants, you must be working in Unity 2022.1 or above.
Material Variants are compatible with existing materials. To create a variant of an existing material, go to your Project window, right-click on the Material Asset and choose Create > Material Variant. You can also convert an existing material into a Material Variant. This is useful if you already have duplicate materials and want to reparent them to a common parent material with the new workflow in your project.
Additionally, you can lock some properties in the parent material to prevent the child material from modifying them (i.e., Surface Options), modify properties in the child that won’t affect the parent material (i.e., Base Map color), and modify the parent to apply some changes that need to be applied to all children materials (i.e., a new Base Map texture).
At any point, you can check the Material Variant hierarchy to verify the impact of changes made at any level or that you’re making changes at the right level of the hierarchy. Material Variants offer plenty of useful functionalities like reparenting, reverting, or applying overrides upward. To learn more, read our documentation here.
We have yet to plan for any new features at this stage, and are currently concentrating on stability. You can share your feedback with us or ask questions on our dedicated Unity forum thread.
Once you’ve tinkered with the system, please take a few minutes to fill out our survey, as this will help us validate the quality of our initial design. To propose new features and vote for upcoming features under consideration, check out our public roadmaps available here.