动态更改Shader属性值

作者:追风剑情 发布于:2016-2-15 17:36 分类:Shader

一、创建脚本

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"
}

三、工程截图

111.png

2222.png


四、运行效果

3333.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号