unity 屏幕淡入淡出效果实现

private GameObject image; //设置图片
private RawImage rawImage;//设置rawimage
public float speed = 3f;
//屏幕是否要逐渐清晰(默认是需要的)
private bool isclear = true;
//屏幕是否需要逐渐变暗(默认是不需要的)
private bool isblack = false;
public override void Awake()
{
    base.Awake();
    image = this.gameObject;
    rawImage = this.GetComponent();
}

/// 
/// 淡入效果
/// 
public void fadetoClear()
{
    rawImage.color=Color.Lerp(rawImage.color,Color.clear, speed*Time.deltaTime);
}
/// 
/// 淡出效果
/// 
public void fadetoBlack()
{
    rawImage.color = Color.Lerp(rawImage.color, Color.black, speed * Time.deltaTime);
}

public void SceneToClear()
{
    fadetoClear();
    if (rawImage.color.a<0.05f)
    {
        rawImage.color=Color.clear;
        rawImage.enabled = false;
        isclear = false;
    }
}
public void SceneToBlack()
{
    rawImage.enabled = true;
    fadetoBlack();   
    if (rawImage.color.a > 0.95f)
    {
        rawImage.color = Color.black;
        isblack = true;
    }
}

/// 
/// 设置场景的淡入
/// 
public void setSceneToClear()
{
    isclear = true;
    isblack = false;
}

/// 
/// 设置场景的淡出
/// 
public void setSceneToBlack()
{
    isclear = false;
    isblack = true;
}

void Update () {

    if (isclear)
    {
        SceneToClear();
    }
    else if (isblack)
    {
        SceneToBlack();
    }
  
}

你可能感兴趣的:(unity 屏幕淡入淡出效果实现)