ColorCtr控制顏色漸變
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方法具體實現、、、
ColorCtr控制顏色漸變