Class MaterialHelper
Utility methods for working with Unity materials and shaders. Provides safe, convenient methods for common material operations.
public static class MaterialHelper
- Inheritance
-
MaterialHelper
- Inherited Members
Methods
ConfigureMaterial(Material, Action<Material>)
Sets multiple shader properties at once using a configuration action.
public static void ConfigureMaterial(Material material, Action<Material> configurator)
Parameters
materialMaterialThe material to modify.
configuratorAction<Material>Action to configure material properties.
Examples
MaterialHelper.ConfigureMaterial(material, mat => {
MaterialHelper.SetColor(mat, "_BaseColor", Color.red);
MaterialHelper.SetFloat(mat, "_Metallic", 0.8f);
MaterialHelper.SetFloat(mat, "_Smoothness", 0.5f);
});
CreateMetallicVariant(Material, Color, float, float)
Creates a metallic material variant from an existing material. Removes all textures and applies metallic properties.
public static Material CreateMetallicVariant(Material baseMaterial, Color metalColor, float metallic = 0.8, float smoothness = 0.5)
Parameters
baseMaterialMaterialThe material to clone and modify.
metalColorColorThe color to apply to the metal.
metallicfloatMetallic value (0.0 to 1.0, default 0.8).
smoothnessfloatSmoothness value (0.0 to 1.0, default 0.5).
Returns
- Material
A new material with metallic properties.
Examples
var metalMaterial = MaterialHelper.CreateMetallicVariant(
originalMaterial,
new Color(0.5f, 0.5f, 0.55f), // Gray metal
metallic: 0.8f,
smoothness: 0.5f
);
RemoveAllTextures(Material)
Removes all textures from a material. Iterates through all shader properties and sets texture properties to null.
public static void RemoveAllTextures(Material material)
Parameters
materialMaterialThe material to modify.
ReplaceMaterials(GameObject, Func<Material, bool>, Action<Material>)
Replaces materials on a GameObject and all its children based on a predicate. Creates new material instances to avoid modifying shared materials.
public static void ReplaceMaterials(GameObject gameObject, Func<Material, bool> predicate, Action<Material> materialModifier)
Parameters
gameObjectGameObjectThe GameObject to process (will include all children).
predicateFunc<Material, bool>Function that returns true for materials to replace.
materialModifierAction<Material>Action to modify the matched materials.
Examples
// Replace all materials with "wood" in the name
MaterialHelper.ReplaceMaterials(
myObject,
mat => mat.name.ToLower().Contains("wood"),
mat => {
MaterialHelper.SetColor(mat, "_BaseColor", Color.red);
MaterialHelper.SetFloat(mat, "_Metallic", 0.8f);
}
);
SetColor(Material, string, Color)
Sets a color property on a material, checking if the property exists first.
public static bool SetColor(Material material, string propertyName, Color color)
Parameters
materialMaterialThe material to modify.
propertyNamestringThe shader property name (e.g., "_BaseColor").
colorColorThe color value to set.
Returns
- bool
True if the property was set, false if it doesn't exist.
SetFloat(Material, string, float)
Sets a float property on a material, checking if the property exists first.
public static bool SetFloat(Material material, string propertyName, float value)
Parameters
materialMaterialThe material to modify.
propertyNamestringThe shader property name (e.g., "_Metallic").
valuefloatThe float value to set.
Returns
- bool
True if the property was set, false if it doesn't exist.
SetTexture(Material, string, Texture)
Sets a texture property on a material, checking if the property exists first.
public static bool SetTexture(Material material, string propertyName, Texture texture)
Parameters
materialMaterialThe material to modify.
propertyNamestringThe shader property name (e.g., "_MainTex").
textureTextureThe texture to set (null to clear).
Returns
- bool
True if the property was set, false if it doesn't exist.