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 updatevoid Start(){}// Update is called once per framevoid 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;}
}