1、在图层新建个空的图层
场景2
2创建c#脚本
脚本创建好后,将脚本推拽进空白图层里面,将黑色图片拉进去脚本的参数里面,如上面最后一张图,两个切换的场景都要进行上述操作
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class FadeInOut : MonoBehaviour
{
//脚本传进来的图片
public Texture img;
//透明度
public static float alpha = 0;
//淡出
public static bool fadeOut = false;
//淡入
public static bool fadeIn = false;
//前端传过来的速度
public float speed;
//场景
private static string scene;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
//渲染页面调用的得是这个方法
private void OnGUI()
{
// Time.deltaTime 上一帧与当前帧间隔帧数
if (fadeOut){
alpha += speed * Time.deltaTime;
if (alpha>1) {
fadeOut = false;
fadeIn = true;
//场景切换
SceneManager.LoadScene(scene);
}
}
if (fadeIn) {
alpha -= speed * Time.deltaTime;
if (alpha<0) {
fadeIn = false;
}
}
//调整透明度
GUI.color = new Color(GUI.color.r,GUI.color.g,GUI.color.b,alpha);
//把场景绘制一张黑色图片
GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),img);
}
public static void SwitchScene(string newScene)
{
fadeOut = true;
scene = newScene;
}
}