Files
beyond/Assets/Scripts/PostProcessController.cs
2024-11-20 15:21:28 +01:00

83 lines
2.7 KiB
C#
Raw Blame History

using UnityEngine;
using System.Collections;
using UnityEngine.Rendering;
public class PostProcessController : MonoBehaviour
{
public Volume volume; // Publiczna referencja do komponentu Volume
public float duration = 1f; // Publiczna warto<74><6F> okre<72>laj<61>ca czas p<>ynnej zmiany wagi
private Coroutine currentCoroutine; // Referencja do aktualnie dzia<69>aj<61>cej coroutiny
private void Awake()
{
// Je<4A>li Volume nie zosta<74> przypisany w inspektorze, pr<70>bujemy go znale<6C><65> na tym samym obiekcie
if (volume == null)
{
volume = GetComponent<Volume>();
if (volume == null)
{
Debug.LogError("Brak komponentu Volume na obiekcie!");
}
}
}
// Funkcja p<>ynnie w<><77>cza efekt post-processingu (od 0 do 1) w czasie ustawionym w duration
public void EnablePostProcessing()
{
if (volume != null)
{
// Je<4A>li ju<6A> jest jaka<6B> coroutina w trakcie, zatrzymujemy j<>
if (currentCoroutine != null)
{
StopCoroutine(currentCoroutine);
}
currentCoroutine = StartCoroutine(ChangeWeightOverTime(0f, 1f, duration));
}
}
// Funkcja p<>ynnie wy<77><79>cza efekt post-processingu (od 1 do 0) w czasie ustawionym w duration
public void DisablePostProcessing()
{
if (volume != null)
{
// Je<4A>li ju<6A> jest jaka<6B> coroutina w trakcie, zatrzymujemy j<>
if (currentCoroutine != null)
{
StopCoroutine(currentCoroutine);
}
if (!gameObject.activeSelf)
gameObject.SetActive(true);
currentCoroutine = StartCoroutine(ChangeWeightOverTime(1f, 0f, duration));
}
}
// Coroutine do p<>ynnej zmiany wagi Volume
private IEnumerator ChangeWeightOverTime(float startWeight, float endWeight, float duration)
{
float elapsed = 0f;
while (elapsed < duration)
{
// Interpolacja mi<6D>dzy startWeight a endWeight
volume.weight = Mathf.Lerp(startWeight, endWeight, elapsed / duration);
elapsed += Time.deltaTime;
yield return null; // Czekamy do nast<73>pnej klatki
}
// Upewniamy si<73>, <20>e waga jest ustawiona na ko<6B>cow<6F> warto<74><6F> po zako<6B>czeniu interpolacji
volume.weight = endWeight;
currentCoroutine = null;
}
// Funkcja ustawia okre<72>lon<6F> wag<61> efektu post-processingu natychmiast (od 0.0 do 1.0)
public void SetPostProcessingWeight(float newWeight)
{
if (volume != null)
{
volume.weight = Mathf.Clamp01(newWeight);
}
}
}