using UnityEngine; using UnityEngine.UI; using Cysharp.Threading.Tasks; using System; using MotionFramework; using MotionFramework.Scripts.Runtime.Engine.Engine.Network.WebRequest; namespace Framework.Tools { /// /// 工具组件 /// /// 功能: /// 黑屏过渡->BlackScreenTransition /// public class ToolsComponent : MonoBehaviour { // 单例实例 private static ToolsComponent _instance; public static ToolsComponent Instance { get { if (_instance == null) { _instance = FindObjectOfType(); if (_instance == null) { Debug.LogError("未找到挂载 ToolsComponent 脚本的对象,请检查场景!"); } else { DontDestroyOnLoad(_instance.gameObject); } } return _instance; } } [SerializeField] private RawImage _image; private void Start() { //BlackScreenTransition(); } /// /// 黑屏过渡 /// public async void BlackScreenTransition() { // 从 0 渐变到 1 await FadeImageAlpha(0f, 1f, 1f); await UniTask.Delay(1000); // 从 1 渐变到 0 await FadeImageAlpha(1f, 0f, 1f); } private async UniTask FadeImageAlpha(float startAlpha, float endAlpha, float duration) { float elapsedTime = 0f; Color currentColor = _image.color; while (elapsedTime < duration) { // 根据时间计算当前透明度 float alpha = Mathf.Lerp(startAlpha, endAlpha, elapsedTime / duration); currentColor.a = alpha; _image.color = currentColor; _image.color = currentColor; elapsedTime += Time.deltaTime; await UniTask.Yield(); } // 确保最终透明度正确 currentColor.a = endAlpha; _image.color = currentColor; } } }