Legacy Documentation: Version 4.6.2
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Material.Material

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public function Material(contents: string)
public Material(string contents);
public def Material(contents as string)

Description

Create a temporary material from a shader source string.

If you have a script which implements a custom special effect, you implement all the graphic setup using shaders & materials. Use this function to create a custom shader & material inside your script. After creating the material, use SetColor, SetTexture, SetFloat, SetVector, SetMatrix to populate the shader property values.

	// Creates an additive-blended material and uses it for rendering
	var color = Color.white;
	function Start () {
		var shaderText =
			"Shader \"Alpha Additive\" {" +
			"Properties { _Color (\"Main Color\", Color) = (1,1,1,0) }" +
			"SubShader {" +
			"	Tags { \"Queue\" = \"Transparent\" }" +
			"	Pass {" +
			"		Blend One One ZWrite Off ColorMask RGB" +
			"		Material { Diffuse [_Color] Ambient [_Color] }" +
			"		Lighting On" +
			"		SetTexture [_Dummy] { combine primary double, primary }" +
			"	}" +
			"}" +
			"}";
		renderer.material = new Material( shaderText );
		renderer.material.color = color;
	}

NOTE: Creating materials this way supports only simple shaders (fixed function ones). If you need a surface shader, or vertex/pixel shaders, you'll need to create shader asset in the editor and use that.

See Also: ShaderLab documentation.

public function Material(shader: Shader)
public Material(Shader shader);
public def Material(shader as Shader)

Description

Create a temporary Material from a Shader.

If you have a script which implements a custom special effect, you implement all the graphic setup using shaders & materials. Use this function to create a custom shader & material inside your script. After creating the material, use SetColor, SetTexture, SetFloat, SetVector, SetMatrix to populate the shader property values.

	// Creates a material from shader&texture references
	var shader : Shader;
	var texture : Texture;
	var color : Color;
	function Start () {
		renderer.material = new Material (shader);
		renderer.material.mainTexture = texture;
		renderer.material.color = color;
	}
	// Creates a cube and assigns a material with a builtin Specular shader.
	function Start () {
		var cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
		cube.renderer.material = new Material (Shader.Find("Specular"));
	}
public function Material(source: Material)
public Material(Material source);
public def Material(source as Material)

Description

Create a temporary Material by copying the shader and all properties from the source Material.