Advertisement
theTANCO

AudioController.cs

Apr 15th, 2021 (edited)
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.37 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AudioController : MonoBehaviour
  6. {
  7.   // This is an audio controller script that makes it easier to control multiple audio sources with
  8.   // animations. This can only control one audio source per frame.
  9.   // This is meant to be used in animation clips.
  10.  
  11.   // First set the size of the Audio Clips array in the inspector to the number of audio clips you
  12.   // intend to use and add audio clips to the array. Audio sources will be automatically created
  13.   // based on this. In your animation clip, set a keyframe for the Active Audio to the element
  14.   // number of the audio source you want to control. In the same keyframe, set all of the properties
  15.   // you want to change. You have to do set a keyframe for Active Audio for every change you make,
  16.   // or set the animations curves to constant and only set a keyframe when you want to change the
  17.   // audio source you want to control.
  18.  
  19.   AudioSource[] audioSources; // Audio sources are created based on the number of audio clips provided.
  20.   public AudioClip[] audioClips; // Audio clips are provided using the inspector.
  21.  
  22.   public int activeAudio; // The audio source currently being controlled.
  23.   public bool playAudio; // When set to true, plays the clip from the currently Active Audio source.
  24.  
  25.   // Below are the same properties that would exist in your audio source.
  26.   // You can set the default initial values for all audio sources in the inspector, or change
  27.   // the values of the properties of each audio source at runtime using your animation clip.
  28.  
  29.   // Main properties visible to the inspector of an Audio Source component.
  30.   [Header("Audio Source Settings")]
  31.   public AudioClip clip;
  32.   AudioClip previousClip;
  33.   public bool mute;
  34.   public bool bypassEffects;
  35.   public bool bypassListenerEffects;
  36.   public bool bypassReverbZones;
  37.   public bool playOnAwake;
  38.   public bool loop;
  39.   [Range(0,256)]
  40.   public int priority;
  41.   [Range(0.0f,1.0f)]
  42.   public float volume;
  43.   [Range(-3.0f,3.0f)]
  44.   public float pitch;
  45.   [Range(-1.0f,1.0f)]
  46.   public float panStereo;
  47.   [Range(0.0f,1.0f)]
  48.   public float spatialBlend;
  49.   [Range(0.0f,1.1f)]
  50.   public float reverbZoneMix;
  51.  
  52.   // 3D Sound properties visible to the inspector of an Audio Source component.
  53.   [Header("3D Sound Settings")]
  54.   [Range(0.0f,5.0f)]
  55.   public float dopplerLevel;
  56.   [Range(0.0f,360.0f)]
  57.   public float spread;
  58.   public AudioRolloffMode rolloffMode;
  59.   public float minDistance;
  60.   public float maxDistance;
  61.  
  62.   // Properties not visible in the inspector of an Audio Source component, but are made visible here.
  63.   [Header("Other Sound Settings")]
  64.   public bool ignoreListenerVolume;
  65.   public bool spatialize;
  66.   public bool spatializePostEffects;
  67.   public float time;
  68.   float previousTime;
  69.   public int timeSamples;
  70.   float previousTimeSamples;
  71.   public AudioVelocityUpdateMode velocityUpdateMode;
  72.  
  73.   // The classes for these properties don't seem to exist when I load this in Unity. I'll implement them when learn how to use them.
  74.   //public AudioMixer outputAudioMixerGroup;
  75.   //public GamepadSpeakerOutputType gamepadSpeakerOutputType;
  76.  
  77.   // Start is called before the first frame update
  78.   void Start()
  79.   {
  80.     // This creates audio sources based on the number of audio clips.
  81.     audioSources = new AudioSource[audioClips.Length];
  82.     for(int i = 0; i < audioClips.Length; i++){
  83.       audioSources[i] = gameObject.AddComponent<AudioSource>();
  84.       audioSources[i].clip = audioClips[i];
  85.       audioSources[i].enabled = false; // The audio source components gets disabled by default.
  86.     }
  87.   }
  88.  
  89.   // The actual default values for an Audio Source.
  90.   void Reset()
  91.   {
  92.     playOnAwake = true;
  93.     priority = 128;
  94.     volume = 1.0f;
  95.     pitch = 1.0f;
  96.     reverbZoneMix = 1.0f;
  97.     dopplerLevel = 1.0f;
  98.     minDistance = 1.0f;
  99.     maxDistance = 500f;
  100.   }
  101.  
  102.   // Update is called once per frame
  103.   void Update()
  104.   {
  105.     // The Audio Source component will be enabled when Play Audio is true to play the audio "on awake".
  106.     // This is done instead of play() or playOneShot() because there is severe random delay
  107.     // in starting the playback of the audio using the latter two methods.
  108.     // My testing shows that turning the component on and off and keeping "Play On Awake" enabled
  109.     // plays the audio correctly 100% of the time.
  110.     // You have the option to disable "Play On Awake", but you may have unwanted results.
  111.     audioSources[activeAudio].enabled = playAudio;
  112.  
  113.     // Main properties.
  114.     if(clip != previousClip){
  115.       audioClips[activeAudio] = clip;
  116.       previousClip = clip;
  117.       audioSources[activeAudio].clip = clip;
  118.     }
  119.     audioSources[activeAudio].mute = mute;
  120.     audioSources[activeAudio].bypassEffects = bypassEffects;
  121.     audioSources[activeAudio].bypassListenerEffects = bypassListenerEffects;
  122.     audioSources[activeAudio].bypassReverbZones = bypassReverbZones;
  123.     audioSources[activeAudio].playOnAwake = playOnAwake;
  124.     audioSources[activeAudio].loop = loop;
  125.     audioSources[activeAudio].priority = priority;
  126.     audioSources[activeAudio].volume = volume;
  127.     audioSources[activeAudio].pitch = pitch;
  128.     audioSources[activeAudio].panStereo = panStereo;
  129.     audioSources[activeAudio].spatialBlend = spatialBlend;
  130.     audioSources[activeAudio].reverbZoneMix = reverbZoneMix;
  131.  
  132.     // 3D Sound properties.
  133.     audioSources[activeAudio].dopplerLevel = dopplerLevel;
  134.     audioSources[activeAudio].spread = spread;
  135.     audioSources[activeAudio].rolloffMode = rolloffMode;
  136.     audioSources[activeAudio].minDistance = minDistance;
  137.     audioSources[activeAudio].maxDistance = maxDistance;
  138.  
  139.     // Properties not in inspector.
  140.     audioSources[activeAudio].ignoreListenerVolume = ignoreListenerVolume;
  141.     audioSources[activeAudio].spatialize = spatialize;
  142.     audioSources[activeAudio].spatializePostEffects = spatializePostEffects;
  143.     if(time != previousTime){
  144.       previousTime = time;
  145.       audioSources[activeAudio].time = time;
  146.     }
  147.     if(timeSamples != previousTimeSamples){
  148.       previousTimeSamples = timeSamples;
  149.       audioSources[activeAudio].timeSamples = timeSamples;
  150.     }
  151.     audioSources[activeAudio].velocityUpdateMode = velocityUpdateMode;
  152.  
  153.     // I'll add these after I learn how they're used.
  154.     // audioSources[activeAudio].outputAudioMixerGroup;
  155.     // audioSources[activeAudio].gamepadSpeakerOutputType;
  156.   }
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement