看到一个场景淡入淡出的脚本文件,分享一下,原文章见来源

 

操作如下:

1.创建一个Shader文件

 

2.双击打开该Shader文件,输入如下代码,更改Shader:


Shader "Unlit/FadeBlack"
{
	Properties{
		_Color("Color",Color)=(0,0,0,1)
	}
	SubShader{
		Tags{"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque"}
		LOD 100
		ZWrite Off
		Blend SrcAlpha OneMinusSrcAlpha
		Color[_Color]
 
		Pass{}
	}
}

 

3.创建一个C#脚本文件,代码如下:

 

 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneFade : MonoBehaviour {
	//淡入时间
	public float fadeInTime = 2.0f;
	//淡出时间
	public float fadeOutTime = 2.0f;
	/// <summary>
	/// bool值为True代表屏幕由白变黑
	/// bool值为False代表屏幕由黑变白
	/// </summary>
	public Action<bool> blackCompleteAction;
	private Color fadeInColor = new Color (0.01f, 0.01f, 0.01f, 1.0f);
	private Color fadeOutColor = new Color (0.01f, 0.01f, 0.01f, 0.01f);
	private Material fadeMaterial = null;
	private bool isFadingIn = false;
	private bool isFadeOut = false;
	private bool isFade = false;
	private YieldInstruction fadeInstruction = new WaitForEndOfFrame ();
	void Awake () {
		fadeMaterial = new Material (Shader.Find ("Unlit/FadeBlack"));
	}
	void OnDisable () {
		isFadeOut = false;
		isFadingIn = false;
	}
	void OnDestroy () {
		if (fadeMaterial != null) {
			isFadeOut = false;
			isFadingIn = false;
			Destroy (fadeMaterial);
		}
	}
	private void OnPostRender () {
		if (isFadingIn) {
			fadeMaterial.SetPass (0);
			GL.PushMatrix ();
			GL.LoadOrtho ();
			GL.Color (fadeMaterial.color);
			GL.Begin (GL.QUADS);
			GL.Vertex3 (0f, 0f, -12f);
			GL.Vertex3 (0f, 1f, -12f);
			GL.Vertex3 (1f, 1f, -12f);
			GL.Vertex3 (1f, 0f, -12f);
			GL.End ();
			GL.PopMatrix ();
			Debug.Log ("isfadein");
		}
		if (isFadeOut) {
			fadeMaterial.SetPass (0);
			GL.PushMatrix ();
			GL.LoadOrtho ();
			GL.Color (fadeMaterial.color);
			GL.Begin (GL.QUADS);
			GL.Vertex3 (0f, 0f, -12f);
			GL.Vertex3 (0f, 1f, -12f);
			GL.Vertex3 (1f, 1f, -12f);
			GL.Vertex3 (1f, 0f, -12f);
			GL.End ();
			GL.PopMatrix ();
			Debug.Log ("isfadeout");
		}
	}
	IEnumerator FadeIEnumator (bool fadeIn) {
		float fadeTime = fadeIn?fadeInTime : fadeOutTime;
		isFadingIn = fadeIn?true : false;
		isFadeOut = fadeIn?false : true;
		float elapsedTime = 0.0f;
		Color color = fadeIn?fadeInColor : fadeOutColor;
		fadeMaterial.color = color;
		while (elapsedTime < fadeTime) {
			yield return new WaitForEndOfFrame ();
			elapsedTime += Time.deltaTime;
			float a = fadeIn?1 - Mathf.Clamp01 (elapsedTime / fadeInTime) : Mathf.Clamp01 (elapsedTime / fadeOutTime);
			color.a = a;
			fadeMaterial.color = color;
		}
		//isFadeOut = isFadingIn = false;
		if (blackCompleteAction != null) {
			blackCompleteAction.Invoke (fadeIn);
		}
	}
	public void Fade (bool isFadeIn) {
		StartCoroutine (FadeIEnumator (isFadeIn));
	}
#if UNITY_EDITOR
	void Update () {
		if (Input.GetMouseButtonDown (0)) {
			Debug.Log ("淡入:画面由暗变亮");
			Fade (true);
		}
		if (Input.GetMouseButtonDown (1)) {
			//表示淡出
			Debug.Log ("淡出:画面由亮变暗");
			Fade (false);
		}
		if (Input.GetMouseButtonDown (2)) {
			isFadingIn = false;
			isFadeOut = false;
		}
	}
#endif
}

 

4.将C#脚本挂载到摄像机上,运行如下(左键点击Game场景)

 

以上为全部详细讲解