Files
Net.Like.Xue.Tokyo/Packages-Local/Com.Project.B.Unity/NodeCanvas/BlackScreenTask.cs

89 lines
2.5 KiB
C#
Raw Normal View History

2025-06-24 23:49:13 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using BITKit;
using BITKit.Tween;
using Cysharp.Threading.Tasks;
using NodeCanvas.Framework;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;
namespace Net.Project.B.NodeCanvas
{
public class BlackScreenTask : ActionTask
{
[RuntimeInitializeOnLoadMethod]
private static void Reload()
{
var newGo = new GameObject("black screen");
var canvas = newGo.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvas.vertexColorAlwaysGammaSpace = true;
Object.DontDestroyOnLoad(newGo);
var newImageGo = new GameObject("image");
newImageGo.transform.SetParent(newGo.transform);
var image = newImageGo.AddComponent<UnityEngine.UI.Image>();
image.color = Color.clear;
image.raycastTarget = false;
image.rectTransform.anchorMin = Vector2.zero;
image.rectTransform.anchorMax = Vector2.one;
image.rectTransform.sizeDelta = Vector2.zero;
image.rectTransform.anchoredPosition = Vector2.zero;
_image = image;
}
private static Image _image;
private static CancellationTokenSource _cancellationTokenSource;
protected override async void OnExecute()
{
_cancellationTokenSource?.Cancel();
_cancellationTokenSource = new CancellationTokenSource();
try
{
await BITween.Lerp(x => _image.color = x, Color.clear, Color.black, 1f, Color.Lerp,
_cancellationTokenSource.Token);
}
catch (OperationCanceledException)
{
return;
}
finally
{
EndAction();
}
try
{
await UniTask.NextFrame();
await UniTask.NextFrame();
await BITween.Lerp(x => _image.color = x, Color.black, Color.clear, 1f, Color.Lerp,
_cancellationTokenSource.Token);
}
catch (OperationCanceledException)
{
}
}
protected override void OnStop(bool interrupted)
{
if (interrupted)
{
_cancellationTokenSource?.Cancel();
}
}
}
}