思路
通过几个Plane颜色变化,实现背景色叠加渐变:
- Plane需要设置半透明
- 颜色值均随机(R、G、B)
- 颜色的过渡使用Color.lerp()实现
1.搭建场景
新建3个Plane
image.png
2.设置Plane半透明
新建3个材质球
image.png
把材质球分别拖拽给Plane对象
image.png
编写脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class BackColor : MonoBehaviour {
Renderer renderer;
byte alpha = 100;
int min = 0;
int max = 255;
byte r,b,g = 0;
Color32 fromColor;
Color32 toColor;
void Awake () {
renderer = this.GetComponent<Renderer> ();
}
void Start(){
fromColor = renderer.material.color;
r = (byte)Random.Range(min,max);
b = (byte)Random.Range(min,max);
g = (byte)Random.Range(min,max);
toColor = new Color32 (r, b, g, alpha);
//renderer.material.color = Color.Lerp (fromColor, Color.red, 5f);
StartCoroutine (changeColor());
}
int time = 3;
public IEnumerator changeColor(){
while (true) {
yield return new WaitForSecondsRealtime (time);
fromColor = renderer.material.color;
r = (byte)Random.Range(min,max);
b = (byte)Random.Range(min,max);
g = (byte)Random.Range(min,max);
toColor = new Color32 (r, b, g, alpha);
Debug.Log (fromColor+" "+toColor);
}
}
void Update () {
renderer.material.color = Color.Lerp(renderer.material.color, toColor, Time.deltaTime * time);
}
}
网友评论