unity 彩虹灯效果


我们可以在 shader中,

添加一个 颜色偏移。

float4 colorA;

void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * colorA;
o.Albedo = c.rgb;

之后,用一个脚本来动态的改变colorA ,

using UnityEngine;
using System.Collections;

public class colorTestScript : MonoBehaviour {
public Color[] colorArray;

private Color currentColor;
    private Color targetColor;

private float timePass;

[Range(0.1f,10)]
public float speedChange = 3;

private int currentColorArrayIndex = 0;

private Color getNextColor()
{
currentColorArrayIndex += 1;
if (currentColorArrayIndex >= colorArray.Length) {
currentColorArrayIndex = 0;
}

return colorArray [currentColorArrayIndex];
}

void Start () {

currentColor = getNextColor();
targetColor = getNextColor();

}
// Update is called once per frame
void Update () {

timePass += Time.deltaTime;
Shader.SetGlobalColor ("colorA",Color.Lerp (currentColor, targetColor, timePass/speedChange));

if (timePass / speedChange >= 1) {
timePass = 0;
currentColor = targetColor;
targetColor = getNextColor();
}


}


}


你可能感兴趣的:(unity)