Table of Contents

Class MaterialHelper

Namespace
S1API.Rendering
Assembly
S1API.dll

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

material Material

The material to modify.

configurator Action<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

baseMaterial Material

The material to clone and modify.

metalColor Color

The color to apply to the metal.

metallic float

Metallic value (0.0 to 1.0, default 0.8).

smoothness float

Smoothness 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

material Material

The 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

gameObject GameObject

The GameObject to process (will include all children).

predicate Func<Material, bool>

Function that returns true for materials to replace.

materialModifier Action<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

material Material

The material to modify.

propertyName string

The shader property name (e.g., "_BaseColor").

color Color

The 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

material Material

The material to modify.

propertyName string

The shader property name (e.g., "_Metallic").

value float

The 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

material Material

The material to modify.

propertyName string

The shader property name (e.g., "_MainTex").

texture Texture

The texture to set (null to clear).

Returns

bool

True if the property was set, false if it doesn't exist.