Files
beyond/Assets/Scripts/VolumeWeightAnimator.cs
2025-06-04 17:31:43 +02:00

72 lines
1.9 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.Rendering;
namespace UnityEngine.Rendering
{
[CurrentPipelineHelpURL("Volumes")]
[ExecuteAlways]
[AddComponentMenu("Miscellaneous/Volume Weight Animator")]
public class VolumeWeightAnimator : MonoBehaviour
{
[SerializeField]
private Volume m_Volume;
[SerializeField]
private float m_AnimationDuration = 5.0f;
[SerializeField]
private AnimationCurve m_WeightCurve = AnimationCurve.Linear(0, 0, 1, 1);
[SerializeField]
private float m_Speed = 1.0f;
private bool wasEnabled = false;
private void Start()
{
if (m_Volume != null)
{
m_Volume.weight = 0;
wasEnabled = m_Volume.enabled;
}
else
{
Debug.LogWarning("Volume is not assigned.");
}
}
private void Update()
{
if (m_Volume != null)
{
if (m_Volume.enabled && !wasEnabled)
{
// Volume zosta³ w³¹czony, uruchamiamy animacjê
StartCoroutine(AnimateWeight());
}
wasEnabled = m_Volume.enabled;
}
}
private IEnumerator AnimateWeight()
{
float elapsedTime = 0f;
m_Volume.weight = 1; // Ustawienie wagi na 1 przy w³¹czaniu Volume
while (elapsedTime < m_AnimationDuration)
{
float t = elapsedTime / m_AnimationDuration;
float curveValue = m_WeightCurve.Evaluate(t);
m_Volume.weight = curveValue; // U¿ywamy bezpoœrednio wartoœci z krzywej
elapsedTime += Time.deltaTime * m_Speed;
yield return null;
}
// Po zakoñczeniu animacji wy³¹czamy Volume
m_Volume.enabled = false;
}
}
}