ColorCtr控制颜色渐变
public class ColorCtr : MonoBehaviour
{
public static global_color Instance; //方便外部调用
public float speed = 1F; //定义速度
public GameObject goRawImage; //定义遮挡Object
private RawImage _image; //定义遮挡图片
private bool _toClear = true; //定义变亮bool
private bool _toBlack = false; //定义变暗bool
//初始化
void Awake()
{
//调用实例
Instance = this;
//判断是否有遮挡物、找到遮挡图片
if (goRawImage)
{
_image = goRawImage.GetComponent<RawImage>();
}
}
//外部调用,改变bool变亮
public void setToClear()
{
_toClear = true;
_toBlack = false;
}
//外部调用,改变bool变暗
public void setToBlack()
{
_toClear = false;
_toBlack = true;
}
//改变颜色插值变亮方法
private void FadeToClear()
{
_image.color = Color.Lerp(_image.color, Color.clear, speed*Time.deltaTime);
}
//改变颜色插值变暗方法
private void FadeToBlack()
{
_image.color = Color.Lerp(_image.color, Color.black, speed * Time.deltaTime);
}
//变亮方法的具体实现
private void SceneToClear()
{
//调用改变颜色变亮插值
FadeToClear();
//颜色彻底改变
if (_image.color.a <= 0.05)
{
_image.color = Color.clear;
_image.enabled = false;
_toClear = false;
}
}
//变暗方法的具体实现
private void SceneToBlack()
{
//打开image
_image.enabled = true;
//调用改变颜色变暗插值
FadeToBlack();
//颜色彻底改变
if (_image.color.a >= 0.95)
{
_image.color = Color.black;
_toClear = false;
}
}
//时刻检测bool变量是否改变,调用方法
void Update()
{
if (_toClear)
{
//调用变亮方法
SceneToClear();
}
else if (_toBlack)
{
//调用变暗方法
SceneToBlack();
}
}
}
注:需要有 goRawImage遮挡物、_image遮挡图片,通过调用setToClear和setToBlack方法具体实现、、、