100 lines
3.2 KiB
C#
100 lines
3.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
using Yarn.Unity;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.Rendering.Universal;
|
|
using System.Collections;
|
|
|
|
public class ScreenEffectManager : MonoBehaviour
|
|
{
|
|
public static ScreenEffectManager Instance { get; private set; }
|
|
|
|
public Image blackoutPanel; // 黑屏面板
|
|
public Image redFlashPanel; // 红色闪烁面板
|
|
private float shakeDuration = 0.5f; // 震屏持续时间
|
|
private float shakeIntensity = 0.04f; // 震屏强度
|
|
|
|
public Volume saturation;
|
|
|
|
private ColorAdjustments colorAdjustments;
|
|
|
|
private Vector3 originalCameraPosition;
|
|
|
|
private void Awake()
|
|
{
|
|
// 确保单例模式
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject); // 可选:保持在场景加载之间
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
// 保存初始相机位置
|
|
originalCameraPosition = Camera.main.transform.localPosition;
|
|
// 获取 ColorAdjustments 设置
|
|
if (saturation.profile.TryGet(out colorAdjustments))
|
|
{
|
|
Debug.Log("ColorAdjustments is available.");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("ColorAdjustments settings not found in PostProcessProfile.");
|
|
}
|
|
}
|
|
|
|
// 震屏效果
|
|
[YarnCommand("shakeScreen")]
|
|
public IEnumerator ShakeScreen()
|
|
{
|
|
yield return Camera.main.transform.DOPunchPosition(new Vector3(1, 0, 0) * shakeIntensity, shakeDuration, 8, 1f).WaitForCompletion();
|
|
}
|
|
|
|
// 黑屏淡入淡出效果
|
|
[YarnCommand("fadeIn")]
|
|
public IEnumerator FadeIn(float waitTime, float duration)
|
|
{
|
|
blackoutPanel.gameObject.SetActive(true);
|
|
Sequence fadeInSequence = DOTween.Sequence();
|
|
fadeInSequence.Append(blackoutPanel.DOFade(1f, duration));
|
|
fadeInSequence.AppendInterval(waitTime);
|
|
fadeInSequence.Append(blackoutPanel.DOFade(0f, duration).OnComplete(() =>
|
|
{
|
|
blackoutPanel.gameObject.SetActive(false);
|
|
}));
|
|
yield return fadeInSequence.WaitForCompletion();
|
|
}
|
|
|
|
// 红色闪烁效果
|
|
[YarnCommand("flashRed")]
|
|
public IEnumerator FlashRed(float duration)
|
|
{
|
|
redFlashPanel.gameObject.SetActive(true);
|
|
yield return redFlashPanel.DOFade(1, duration / 2).WaitForCompletion();
|
|
yield return redFlashPanel.DOFade(0, duration / 2).OnComplete(() =>
|
|
{
|
|
redFlashPanel.gameObject.SetActive(false);
|
|
}).WaitForCompletion();
|
|
}
|
|
|
|
// 动态调整饱和度
|
|
[YarnCommand("Set_Saturation")]
|
|
public IEnumerator TweenSaturation(float targetSaturation, float duration)
|
|
{
|
|
if (colorAdjustments != null)
|
|
{
|
|
yield return DOTween.To(() => colorAdjustments.saturation.value, // 获取当前饱和度值
|
|
x => colorAdjustments.saturation.value = x, // 设置新的饱和度值
|
|
targetSaturation, // 目标饱和度值
|
|
duration).WaitForCompletion(); // 过渡持续时间
|
|
}
|
|
}
|
|
}
|