一. 使用Dotween,建议使用我的方式
FindObjectOfType<SceneFadeInAndOut>().FadeIn(() =>
{
Debug.Log("Fade in MenuStart Scene");
});
using DG.Tweening;
using System;
using System.Numerics;
using UnityEngine;
using UnityEngine.UI;public class SceneFadeInAndOut : MonoBehaviour
{#region 字段和属性的定义//渐变的速率public float m_fTime = 1f;//RawImage组件RawImage _rawImage;Color blackAlpha0 => new Color(0, 0, 0, 0);#endregionprivate void Awake(){_rawImage = FindObjectOfType<RawImage>();}void Update(){if (Input.GetKeyDown(KeyCode.I)){FadeIn(null);}if (Input.GetKeyDown(KeyCode.O)){FadeOut(null);}}/// <summary>/// 设置场景的淡入/// </summary>public void FadeIn(Action complete){_rawImage.gameObject.SetActive(true);_rawImage.color = Color.black;_rawImage.DOColor(blackAlpha0, m_fTime).SetEase(Ease.InOutQuad).OnComplete(() =>{_rawImage.gameObject.SetActive(false);complete?.Invoke();});}/// <summary>/// 设置场景的淡出/// </summary>public void FadeOut(Action complete){_rawImage.gameObject.SetActive(true);_rawImage.color = blackAlpha0;_rawImage.DOColor(Color.black, m_fTime).SetEase(Ease.InOutQuad).OnComplete(() =>{complete?.Invoke();});}}
二. https://www.cnblogs.com/MoRanQianXiao/p/7691790.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class SceneFadeInAndOut : MonoBehaviour
{#region 单例模式//设置静态的字段来模拟全局的变量(开始为空)private static SceneFadeInAndOut _instance = null;/// <summary>/// 只读属性的设置/// </summary>public static SceneFadeInAndOut Instance{get{//如果一开始的为空的话if (_instance == null){//进行类的实例化的操作_instance = new SceneFadeInAndOut();}//下一个访问的话_instance就不为空直接返回字段return _instance;}}private SceneFadeInAndOut() { }#endregion#region 字段和属性的定义//渐变的速率public float floatColorChangeSpeed = 1f;//RawImage对象public GameObject goRawImage;//RawImage组件private RawImage _rawImage;//屏幕是否要逐渐清晰(默认是需要的)private bool _isSceneToClear = true;//屏幕是否需要逐渐变暗(默认是不需要的)private bool _isSceneToBlack = false;#endregionvoid Awake(){//如果goRawImage不为空的话if (goRawImage){//得到RawImage组件_rawImage = goRawImage.GetComponent<RawImage>();}}void Update(){if (_isSceneToClear){SceneToClear();}else if (_isSceneToBlack){SceneToBlack();}if(Input.GetKeyDown(KeyCode.C)){SetSceneToClear();}if (Input.GetKeyDown(KeyCode.D)){SetSceneToBlack();}}#region 公共方法的定义/// <summary>/// 设置场景的淡入/// </summary>public void SetSceneToClear(){InitToBlack();_isSceneToClear = true;_isSceneToBlack = false;}/// <summary>/// 设置场景的淡出/// </summary>public void SetSceneToBlack(){_isSceneToClear = false;_isSceneToBlack = true;}#endregion#region 私有方法的定义/// <summary>/// 屏幕逐渐清晰(淡入)/// </summary>private void FadeToClear(){//插值运算_rawImage.color = Color.Lerp(_rawImage.color, Color.clear, floatColorChangeSpeed * Time.deltaTime);}/// <summary>/// 屏幕逐渐暗淡(淡出)/// </summary>private void FadeToBlack(){//插值运算_rawImage.color = Color.Lerp(_rawImage.color, Color.black, floatColorChangeSpeed * Time.deltaTime);}/// <summary>/// 屏幕的淡入/// </summary>private void SceneToClear(){FadeToClear();//当我们的a值小于等于0.05f的时候 就相当于完全透明了if (_rawImage.color.a <= 0.05f){//设置为完全透明_rawImage.color = Color.clear;//组件的开关设置为关闭的状态_rawImage.enabled = false;//布尔条件设置为false_isSceneToClear = false;}}private void InitToBlack(){//组件的打开_rawImage.enabled = true;//设置为完全不透明的状态 _rawImage.color = Color.black;//布尔条件当到达指定的阿尔法值得时候设置为false_isSceneToBlack = false;}/// <summary>/// 屏幕的淡出/// </summary>private void SceneToBlack(){//组件的打开_rawImage.enabled = true;FadeToBlack();//当前的阿尔法值大于0.95f的时候 表示已经接近于完全不透明的状态if (_rawImage.color.a >= 0.95f){//设置为完全不透明的状态 _rawImage.color = Color.black;//布尔条件当到达指定的阿尔法值得时候设置为false_isSceneToBlack = false;}}#endregion}//class_end