Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AudioController : MonoBehaviour
- {
- // This is an audio controller script that makes it easier to control multiple audio sources with
- // animations. This can only control one audio source per frame.
- // This is meant to be used in animation clips.
- // First set the size of the Audio Clips array in the inspector to the number of audio clips you
- // intend to use and add audio clips to the array. Audio sources will be automatically created
- // based on this. In your animation clip, set a keyframe for the Active Audio to the element
- // number of the audio source you want to control. In the same keyframe, set all of the properties
- // you want to change. You have to do set a keyframe for Active Audio for every change you make,
- // or set the animations curves to constant and only set a keyframe when you want to change the
- // audio source you want to control.
- AudioSource[] audioSources; // Audio sources are created based on the number of audio clips provided.
- public AudioClip[] audioClips; // Audio clips are provided using the inspector.
- public int activeAudio; // The audio source currently being controlled.
- public bool playAudio; // When set to true, plays the clip from the currently Active Audio source.
- // Below are the same properties that would exist in your audio source.
- // You can set the default initial values for all audio sources in the inspector, or change
- // the values of the properties of each audio source at runtime using your animation clip.
- // Main properties visible to the inspector of an Audio Source component.
- [Header("Audio Source Settings")]
- public AudioClip clip;
- AudioClip previousClip;
- public bool mute;
- public bool bypassEffects;
- public bool bypassListenerEffects;
- public bool bypassReverbZones;
- public bool playOnAwake;
- public bool loop;
- [Range(0,256)]
- public int priority;
- [Range(0.0f,1.0f)]
- public float volume;
- [Range(-3.0f,3.0f)]
- public float pitch;
- [Range(-1.0f,1.0f)]
- public float panStereo;
- [Range(0.0f,1.0f)]
- public float spatialBlend;
- [Range(0.0f,1.1f)]
- public float reverbZoneMix;
- // 3D Sound properties visible to the inspector of an Audio Source component.
- [Header("3D Sound Settings")]
- [Range(0.0f,5.0f)]
- public float dopplerLevel;
- [Range(0.0f,360.0f)]
- public float spread;
- public AudioRolloffMode rolloffMode;
- public float minDistance;
- public float maxDistance;
- // Properties not visible in the inspector of an Audio Source component, but are made visible here.
- [Header("Other Sound Settings")]
- public bool ignoreListenerVolume;
- public bool spatialize;
- public bool spatializePostEffects;
- public float time;
- float previousTime;
- public int timeSamples;
- float previousTimeSamples;
- public AudioVelocityUpdateMode velocityUpdateMode;
- // 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.
- //public AudioMixer outputAudioMixerGroup;
- //public GamepadSpeakerOutputType gamepadSpeakerOutputType;
- // Start is called before the first frame update
- void Start()
- {
- // This creates audio sources based on the number of audio clips.
- audioSources = new AudioSource[audioClips.Length];
- for(int i = 0; i < audioClips.Length; i++){
- audioSources[i] = gameObject.AddComponent<AudioSource>();
- audioSources[i].clip = audioClips[i];
- audioSources[i].enabled = false; // The audio source components gets disabled by default.
- }
- }
- // The actual default values for an Audio Source.
- void Reset()
- {
- playOnAwake = true;
- priority = 128;
- volume = 1.0f;
- pitch = 1.0f;
- reverbZoneMix = 1.0f;
- dopplerLevel = 1.0f;
- minDistance = 1.0f;
- maxDistance = 500f;
- }
- // Update is called once per frame
- void Update()
- {
- // The Audio Source component will be enabled when Play Audio is true to play the audio "on awake".
- // This is done instead of play() or playOneShot() because there is severe random delay
- // in starting the playback of the audio using the latter two methods.
- // My testing shows that turning the component on and off and keeping "Play On Awake" enabled
- // plays the audio correctly 100% of the time.
- // You have the option to disable "Play On Awake", but you may have unwanted results.
- audioSources[activeAudio].enabled = playAudio;
- // Main properties.
- if(clip != previousClip){
- audioClips[activeAudio] = clip;
- previousClip = clip;
- audioSources[activeAudio].clip = clip;
- }
- audioSources[activeAudio].mute = mute;
- audioSources[activeAudio].bypassEffects = bypassEffects;
- audioSources[activeAudio].bypassListenerEffects = bypassListenerEffects;
- audioSources[activeAudio].bypassReverbZones = bypassReverbZones;
- audioSources[activeAudio].playOnAwake = playOnAwake;
- audioSources[activeAudio].loop = loop;
- audioSources[activeAudio].priority = priority;
- audioSources[activeAudio].volume = volume;
- audioSources[activeAudio].pitch = pitch;
- audioSources[activeAudio].panStereo = panStereo;
- audioSources[activeAudio].spatialBlend = spatialBlend;
- audioSources[activeAudio].reverbZoneMix = reverbZoneMix;
- // 3D Sound properties.
- audioSources[activeAudio].dopplerLevel = dopplerLevel;
- audioSources[activeAudio].spread = spread;
- audioSources[activeAudio].rolloffMode = rolloffMode;
- audioSources[activeAudio].minDistance = minDistance;
- audioSources[activeAudio].maxDistance = maxDistance;
- // Properties not in inspector.
- audioSources[activeAudio].ignoreListenerVolume = ignoreListenerVolume;
- audioSources[activeAudio].spatialize = spatialize;
- audioSources[activeAudio].spatializePostEffects = spatializePostEffects;
- if(time != previousTime){
- previousTime = time;
- audioSources[activeAudio].time = time;
- }
- if(timeSamples != previousTimeSamples){
- previousTimeSamples = timeSamples;
- audioSources[activeAudio].timeSamples = timeSamples;
- }
- audioSources[activeAudio].velocityUpdateMode = velocityUpdateMode;
- // I'll add these after I learn how they're used.
- // audioSources[activeAudio].outputAudioMixerGroup;
- // audioSources[activeAudio].gamepadSpeakerOutputType;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement