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 }" + " }" + "}" + "}"; var rend = GetComponent.<Renderer>(); rend.material = new Material(shaderText); rend.material.color = color; }
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public Color color = Color.white; void Start() { string 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 rend = GetComponent<Renderer>(); rend.material = new Material(shaderText); rend.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.
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 and texture references. var shader : Shader; var texture : Texture; var color : Color;
function Start() { var rend = GetComponent.<Renderer>(); rend.material = new Material(shader); rend.material.mainTexture = texture; rend.material.color = color; }
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public Shader shader; public Texture texture; public Color color; void Start() { Renderer rend = GetComponent<Renderer>(); rend.material = new Material(shader); rend.material.mainTexture = texture; rend.material.color = color; } }
// Creates a cube and assigns a material with a builtin Specular shader. function Start () { var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); var rend = cube.GetComponent.<Renderer>(); rend.material = new Material(Shader.Find("Specular")); }
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { void Start() { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); Renderer rend = cube.GetComponent<Renderer>(); rend.material = new Material(Shader.Find("Specular")); } }
Create a temporary Material by copying the shader and all properties from the source
Material.