Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Material

Create a temporary Material.
Read time 2 minutesLast updated 5 days ago

Definition

  • Type: Constructor
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

Material(Shader)

Create a temporary Material.
public Material(Shader shader)

Parameters

shader

Create a material with a given Shader.

Remarks

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 Material.SetColor, Material.SetTexture, Material.SetFloat, Material.SetVector, Material.SetMatrix to populate the shader property values.
Additional Resources: Materials, Shaders.

Examples

using UnityEngine;public class Example : MonoBehaviour{ // Creates a material from shader and texture references. Shader shader; Texture texture; Color color; void Start() { Renderer rend = GetComponent<Renderer> (); rend.material = new Material(shader); rend.material.mainTexture = texture; rend.material.color = color; }}
using UnityEngine;public class Example : MonoBehaviour{ // Creates a cube and assigns a material with a builtin Specular shader. void Start() { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); Renderer rend = cube.GetComponent<Renderer> (); rend.material = new Material(Shader.Find("Specular")); }}

Material(Material)

Create a temporary Material.
public Material(Material source)

Parameters

source

Create a material by copying all properties from another material.

Remarks

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 Material.SetColor, Material.SetTexture, Material.SetFloat, Material.SetVector, Material.SetMatrix to populate the shader property values.
Additional Resources: Materials, Shaders.

Examples

using UnityEngine;public class Example : MonoBehaviour{ // Creates a material from shader and texture references. Shader shader; Texture texture; Color color; void Start() { Renderer rend = GetComponent<Renderer> (); rend.material = new Material(shader); rend.material.mainTexture = texture; rend.material.color = color; }}
using UnityEngine;public class Example : MonoBehaviour{ // Creates a cube and assigns a material with a builtin Specular shader. void Start() { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); Renderer rend = cube.GetComponent<Renderer> (); rend.material = new Material(Shader.Find("Specular")); }}