一. 使用Dotween,建议使用我的方式
FindObjectOfType
().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);
- #endregion
-
- private void Awake()
- {
- _rawImage = FindObjectOfType
(); - }
-
-
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.I))
- {
- FadeIn(null);
- }
-
- if (Input.GetKeyDown(KeyCode.O))
- {
- FadeOut(null);
- }
- }
-
- ///
- /// 设置场景的淡入
- ///
- 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();
- });
-
- }
-
- ///
- /// 设置场景的淡出
- ///
- 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;
- ///
- /// 只读属性的设置
- ///
- 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;
-
- #endregion
-
- void Awake()
- {
- //如果goRawImage不为空的话
- if (goRawImage)
- {
- //得到RawImage组件
- _rawImage = goRawImage.GetComponent
(); - }
- }
- void Update()
- {
- if (_isSceneToClear)
- {
- SceneToClear();
- }
- else if (_isSceneToBlack)
- {
- SceneToBlack();
- }
-
- if(Input.GetKeyDown(KeyCode.C))
- {
- SetSceneToClear();
- }
-
- if (Input.GetKeyDown(KeyCode.D))
- {
- SetSceneToBlack();
- }
- }
-
- #region 公共方法的定义
-
- ///
- /// 设置场景的淡入
- ///
- public void SetSceneToClear()
- {
- InitToBlack();
-
- _isSceneToClear = true;
- _isSceneToBlack = false;
- }
-
- ///
- /// 设置场景的淡出
- ///
- public void SetSceneToBlack()
- {
-
- _isSceneToClear = false;
- _isSceneToBlack = true;
- }
-
- #endregion
-
- #region 私有方法的定义
-
- ///
- /// 屏幕逐渐清晰(淡入)
- ///
- private void FadeToClear()
- {
- //插值运算
- _rawImage.color = Color.Lerp(_rawImage.color, Color.clear, floatColorChangeSpeed * Time.deltaTime);
- }
-
- ///
- /// 屏幕逐渐暗淡(淡出)
- ///
- private void FadeToBlack()
- {
- //插值运算
- _rawImage.color = Color.Lerp(_rawImage.color, Color.black, floatColorChangeSpeed * Time.deltaTime);
- }
-
- ///
- /// 屏幕的淡入
- ///
- 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;
- }
-
- ///
- /// 屏幕的淡出
- ///
- 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