一、创建脚本
using UnityEngine;
using System.Collections;
public class ProceduralMaterialTest : MonoBehaviour {
public Color mainColor;
void Start ()
{
renderer.materials[0].SetColor("_MainColor", mainColor);
}
}
二、创建Shader
Shader "Custom/CubeShader" {
Properties {
_MainColor ("Main Color", Color) = (1, 1, 1, 1)
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
float4 _MainColor;
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = _MainColor.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
三、工程截图
四、运行效果