学习unity的有趣时刻 2.淡出淡入

【2.关于载入场景时的淡出淡入效果】 2017.11.30

首先需要一张纯黑色的图片

然后新建一个空物体,添加GUI Texture组件,将颜色调成纯黑并在Texture这一栏添加刚刚找到的纯黑色图片

学习unity的有趣时刻 2.淡出淡入_第1张图片

为空物体添加一个脚本,脚本的代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class FadedInOut : MonoBehaviour {

private float FadeSpeed = 1.5f;

private bool sceneStarting = true;
private GUITexture tex;

void Start()
{
    tex = this.GetComponent ();
    tex.pixelInset = new Rect (0, 0, Screen.width, Screen.height);
}

void Update()
{
    if (sceneStarting) {
        StartScene ();
    }
}

private void FadeToClear()
{
    tex.color = Color.Lerp (tex.color, Color.clear, FadeSpeed * Time.deltaTime);
}

private void FadeToBlack()
{
    tex.color = Color.Lerp (tex.color, Color.black, FadeSpeed * Time.deltaTime);
}

private void StartScene()
{
    FadeToClear ();

    if (tex.color.a <= 0.05f) {

        tex.color = Color.clear;
        tex.enabled = false;
        sceneStarting = false;

    }
}

public void EndScene()
{
    tex.enabled = true;
    FadeToBlack ();

    if (tex.color.a >= 0.95f) {

        SceneManager.LoadScene ("demo");
        //要使用这个方法需要UnityEngine.SceneManagement的支持
    }
}
}

你可能感兴趣的:(unity3d)