Iuvo Unity 0.5.1
This is library containing a variety of helper classes and extension methods for the Unity gane engine.
GitHub | Iuvo Unity Updates | Creator
Loading...
Searching...
No Matches
IuvoUnity.Extensions.MaterialExtensions Class Reference

Provides extension methods for working with Unity Materials. More...

Static Public Member Functions

static void WithMainColor (this Material material, Color color)
 Sets the main color of the material, if the "_Color" property exists.
static void WithMainTexture (this Material material, Texture texture)
 Sets the main texture of the material, if the "_MainTex" property exists.
static void WithMainTextureOffset (this Material material, Vector2 offset)
 Sets the offset of the main texture, if the "_MainTex" property exists.
static void WithMainTextureScale (this Material material, Vector2 scale)
 Sets the scale of the main texture, if the "_MainTex" property exists.
static void WithMainTextureTiling (this Material material, Vector2 tiling)
 Sets the tiling (scale) of the main texture.
static void WithMetallic (this Material material, float metallic)
 Sets the metallic value of the material, clamped between 0 and 1.
static void WithEmissionColor (this Material material, Color color)
 Sets the emission color of the material, if the "_EmissionColor" property exists.
static void WithShader (this Material material, Shader shader)
 Sets the shader of the material.
static void WithShader (this Material material, string shaderName)
static void WithShader (this Material material, string shaderName, string fallbackShaderName)
 Sets the shader of the material by name, falling back to a secondary shader if the first is not found.
static void WithRenderingMode (this Material material, int mode)
 Sets the rendering mode of the material using the "_Mode" property.
static void WithBuffer (this Material material, string propertyName, ComputeBuffer buffer)
 Sets a compute buffer on the material.
static void WithColorArray (this Material material, string propertyName, Color[] colors)
 Sets a color array on the material.
static void WithFloatArray (this Material material, string propertyName, float[] values)
 Sets a float array on the material.
static void WithMatrixArray (this Material material, string propertyName, Matrix4x4[] matrices)
 Sets a matrix array on the material.
static void WithTextureArray (this Material material, string propertyName, Texture2DArray textures)
 Sets a texture array (Texture2DArray) on the material.
static void WithVectorArray (this Material material, string propertyName, Vector4[] vectors)
 Sets a vector array on the material.

Detailed Description

Provides extension methods for working with Unity Materials.

Member Function Documentation

◆ WithBuffer()

void IuvoUnity.Extensions.MaterialExtensions.WithBuffer ( this Material material,
string propertyName,
ComputeBuffer buffer )
static

Sets a compute buffer on the material.

Parameters
materialThe material to modify.
propertyNameThe shader property name.
bufferThe compute buffer.
177 {
178 if (material.HasProperty(propertyName))
179 material.SetBuffer(propertyName, buffer);
180 else
181 IuvoDebug.DebugLogWarning($"Material '{material.name}' does not have property '{propertyName}' for ComputeBuffer.");
182 }

References IuvoUnity.Debug.IuvoDebug.DebugLogWarning().

◆ WithColorArray()

void IuvoUnity.Extensions.MaterialExtensions.WithColorArray ( this Material material,
string propertyName,
Color[] colors )
static

Sets a color array on the material.

Parameters
materialThe material to modify.
propertyNameThe shader property name.
colorsThe array of colors.
191 {
192 if (material.HasProperty(propertyName))
193 material.SetColorArray(propertyName, colors);
194 }

◆ WithEmissionColor()

void IuvoUnity.Extensions.MaterialExtensions.WithEmissionColor ( this Material material,
Color color )
static

Sets the emission color of the material, if the "_EmissionColor" property exists.

Parameters
materialThe material to modify.
colorThe emission color.
88 {
89 if (material.HasProperty("_EmissionColor"))
90 material.SetColor("_EmissionColor", color);
91 }

◆ WithFloatArray()

void IuvoUnity.Extensions.MaterialExtensions.WithFloatArray ( this Material material,
string propertyName,
float[] values )
static

Sets a float array on the material.

Parameters
materialThe material to modify.
propertyNameThe shader property name.
valuesThe array of float values.
203 {
204 if (material.HasProperty(propertyName))
205 material.SetFloatArray(propertyName, values);
206 }

◆ WithMainColor()

void IuvoUnity.Extensions.MaterialExtensions.WithMainColor ( this Material material,
Color color )
static

Sets the main color of the material, if the "_Color" property exists.

Parameters
materialThe material to modify.
colorThe color to apply.
19 {
20 if (material.HasProperty("_Color"))
21 material.SetColor("_Color", color);
22 else
23 IuvoDebug.DebugLogWarning($"Material '{material.name}' does not have a _Color property.");
24 }

References IuvoUnity.Debug.IuvoDebug.DebugLogWarning().

◆ WithMainTexture()

void IuvoUnity.Extensions.MaterialExtensions.WithMainTexture ( this Material material,
Texture texture )
static

Sets the main texture of the material, if the "_MainTex" property exists.

Parameters
materialThe material to modify.
textureThe texture to assign.
32 {
33 if (material.HasProperty("_MainTex"))
34 material.SetTexture("_MainTex", texture);
35 else
36 IuvoDebug.DebugLogWarning($"Material '{material.name}' does not have a _MainTex property.");
37 }

References IuvoUnity.Debug.IuvoDebug.DebugLogWarning().

◆ WithMainTextureOffset()

void IuvoUnity.Extensions.MaterialExtensions.WithMainTextureOffset ( this Material material,
Vector2 offset )
static

Sets the offset of the main texture, if the "_MainTex" property exists.

Parameters
materialThe material to modify.
offsetThe texture offset.
45 {
46 if (material.HasProperty("_MainTex"))
47 material.SetTextureOffset("_MainTex", offset);
48 }

◆ WithMainTextureScale()

void IuvoUnity.Extensions.MaterialExtensions.WithMainTextureScale ( this Material material,
Vector2 scale )
static

Sets the scale of the main texture, if the "_MainTex" property exists.

Parameters
materialThe material to modify.
scaleThe texture scale.
56 {
57 if (material.HasProperty("_MainTex"))
58 material.SetTextureScale("_MainTex", scale);
59 }

◆ WithMainTextureTiling()

void IuvoUnity.Extensions.MaterialExtensions.WithMainTextureTiling ( this Material material,
Vector2 tiling )
static

Sets the tiling (scale) of the main texture.

Parameters
materialThe material to modify.
tilingThe tiling vector.
67 {
68 material.WithMainTextureScale(tiling);
69 }

◆ WithMatrixArray()

void IuvoUnity.Extensions.MaterialExtensions.WithMatrixArray ( this Material material,
string propertyName,
Matrix4x4[] matrices )
static

Sets a matrix array on the material.

Parameters
materialThe material to modify.
propertyNameThe shader property name.
matricesThe array of matrices.
215 {
216 if (material.HasProperty(propertyName))
217 material.SetMatrixArray(propertyName, matrices);
218 }

◆ WithMetallic()

void IuvoUnity.Extensions.MaterialExtensions.WithMetallic ( this Material material,
float metallic )
static

Sets the metallic value of the material, clamped between 0 and 1.

Parameters
materialThe material to modify.
metallicThe metallic value.
77 {
78 if (material.HasProperty("_Metallic"))
79 material.SetFloat("_Metallic", Mathf.Clamp01(metallic));
80 }

◆ WithRenderingMode()

void IuvoUnity.Extensions.MaterialExtensions.WithRenderingMode ( this Material material,
int mode )
static

Sets the rendering mode of the material using the "_Mode" property.

Parameters
materialThe material to modify.
modeThe rendering mode (typically 0=Opaque, 1=Cutout, 2=Fade, 3=Transparent).
159 {
160 if (material.HasProperty("_Mode"))
161 {
162 material.SetInt("_Mode", mode);
163 }
164 else
165 {
166 IuvoDebug.DebugLogWarning($"Material '{material.name}' does not support '_Mode'. Use a compatible shader for transparency.");
167 }
168 }

References IuvoUnity.Debug.IuvoDebug.DebugLogWarning().

◆ WithShader() [1/3]

void IuvoUnity.Extensions.MaterialExtensions.WithShader ( this Material material,
Shader shader )
static

Sets the shader of the material.

Parameters
materialThe material to modify.
shaderThe shader to assign.
99 {
100 if (shader != null)
101 {
102 material.shader = shader;
103 }
104 else
105 {
106 IuvoDebug.DebugLogWarning($"Null shader passed to material '{material.name}'. Shader not changed.");
107 }
108 }

References IuvoUnity.Debug.IuvoDebug.DebugLogWarning().

◆ WithShader() [2/3]

void IuvoUnity.Extensions.MaterialExtensions.WithShader ( this Material material,
string shaderName )
static

<summary Sets the shader of the material by shader name.

Parameters
materialThe material to modify.
shaderNameThe name of the shader.
116 {
117 Shader shader = Shader.Find(shaderName);
118 if (shader != null)
119 {
120 material.shader = shader;
121 }
122 else
123 {
124 IuvoDebug.DebugLogWarning($"Shader '{shaderName}' not found. Material '{material.name}' was not updated.");
125 }
126 }

References IuvoUnity.Debug.IuvoDebug.DebugLogWarning().

◆ WithShader() [3/3]

void IuvoUnity.Extensions.MaterialExtensions.WithShader ( this Material material,
string shaderName,
string fallbackShaderName )
static

Sets the shader of the material by name, falling back to a secondary shader if the first is not found.

Parameters
materialThe material to modify.
shaderNameThe primary shader name.
fallbackShaderNameThe fallback shader name.
135 {
136 Shader shader = Shader.Find(shaderName);
137 if (shader == null)
138 {
139 IuvoDebug.DebugLogWarning($"Shader '{shaderName}' not found. Falling back to '{fallbackShaderName}'.");
140 shader = Shader.Find(fallbackShaderName);
141 }
142
143 if (shader != null)
144 {
145 material.shader = shader;
146 }
147 else
148 {
149 IuvoDebug.DebugLogError($"Both shader '{shaderName}' and fallback '{fallbackShaderName}' not found. Material '{material.name}' shader not changed.");
150 }
151 }

References IuvoUnity.Debug.IuvoDebug.DebugLogError(), and IuvoUnity.Debug.IuvoDebug.DebugLogWarning().

◆ WithTextureArray()

void IuvoUnity.Extensions.MaterialExtensions.WithTextureArray ( this Material material,
string propertyName,
Texture2DArray textures )
static

Sets a texture array (Texture2DArray) on the material.

Parameters
materialThe material to modify.
propertyNameThe shader property name.
texturesThe texture array to assign.
227 {
228 if (material.HasProperty(propertyName))
229 material.SetTexture(propertyName, textures);
230 else
231 IuvoDebug.DebugLogWarning($"Material '{material.name}' does not support texture arrays at '{propertyName}'.");
232 }

References IuvoUnity.Debug.IuvoDebug.DebugLogWarning().

◆ WithVectorArray()

void IuvoUnity.Extensions.MaterialExtensions.WithVectorArray ( this Material material,
string propertyName,
Vector4[] vectors )
static

Sets a vector array on the material.

Parameters
materialThe material to modify.
propertyNameThe shader property name.
vectorsThe array of vectors.
241 {
242 if (material.HasProperty(propertyName))
243 material.SetVectorArray(propertyName, vectors);
244 }

The documentation for this class was generated from the following file: