Advertisement
Guest User

Sample-accurate volume ramp (Unity)

a guest
Jul 25th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.54 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class VolRamp : MonoBehaviour {
  5.    
  6.     public float rampTime = 1f; // in seconds
  7.  
  8.     private float _volume = 0f;
  9.     private float _volumeDelta;
  10.  
  11.  
  12.     void Awake () {
  13.         AudioSource audioSource = GetComponent<AudioSource>();
  14.         _volumeDelta = 1f / ((float)audioSource.clip.frequency * rampTime);
  15.     }
  16.  
  17.     void OnAudioFilterRead (float[] data, int numChannels) {
  18.         for (int i = 0; i < data.Length; ++i) {
  19.             if (_volume < 1f) {
  20.                 _volume += _volumeDelta;
  21.             }
  22.  
  23.             data[i] *= _volume;
  24.         }
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement