Advertisement
Staggart

Unity Post Process Volume blend animation

Oct 10th, 2022 (edited)
1,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.Rendering.PostProcessing;
  4.  
  5. public class VolumeBlendAnimation : MonoBehaviour
  6. {
  7.     [Tooltip("Assign a volume that has effects set up on it\n\nThey need to be at full intensity")]
  8.     public PostProcessVolume volume;
  9.     [Tooltip("In case the volume should be disabled during edit mode, enable this checkbox to automatically enable it when entering play mode")]
  10.     public bool enableVolumeOnAwake;
  11.     public float fadeTime = 1f;
  12.  
  13.     [Header("Debug")]
  14.     [Range(0f, 1f)]
  15.     public float progress;
  16.    
  17.     void OnEnable()
  18.     {
  19.         if(enableVolumeOnAwake) volume.enabled = true;
  20.        
  21.         StartCoroutine(Fade(false));
  22.     }
  23.  
  24.     private void OnDisable()
  25.     {
  26.         StartCoroutine(Fade(true));
  27.     }
  28.    
  29.     IEnumerator Fade(bool invert)
  30.     {
  31.         var elapsedTime = 0f;
  32.  
  33.         while (elapsedTime < fadeTime)
  34.         {
  35.             elapsedTime += Time.deltaTime;
  36.            
  37.             SetProgress((invert ? 1f-(elapsedTime / fadeTime) : (elapsedTime / fadeTime)));
  38.  
  39.             yield return null;
  40.         }
  41.     }
  42.  
  43.     private void SetProgress(float t)
  44.     {
  45.         progress = Mathf.Clamp01(t);
  46.        
  47.         volume.weight = progress;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement