Hi nicmar, it seems as though your creating an animation clip with an animator controller as well. This is a pretty old tutorial, before Mecanim, so I recorded a small video to show how the animation clip was created for the audio fade:
That should correct question #1, as for question 2, the tutorial does not support changing the pitch, the is set default at 1.0f, which is no change in the pitch of the sound. Check out the video and basically you could do the same for the pitch instead of the volume, and with some small changes to code, you can essentially alter the pitch of the audio.
// over loads
public static GameObject CreateGetFadeAudioObject(AudioClip aClip) {
return CreateGetFadeAudioObject(aClip, true);
}
public static GameObject CreateGetFadeAudioObject(AudioClip aClip, bool dLoop) {
return CreateGetFadeAudioObject(aClip, dLoop, null);
}
public static GameObject CreateGetFadeAudioObject(AudioClip aClip, bool dLoop, AnimationClip fadeClip) {
return CreateGetFadeAudioObject(aClip, dLoop, fadeClip, "ReturnedAudioPlayObject");
}
// Additions for fading pitch
public static GameObject CreateGetFadeAudioObject(AudioClip aClip, bool dLoop, AnimationClip fadeClip, AnimationClip fadePitch) {
return CreateGetFadeAudioObject(aClip, dLoop, fadeClip, fadePitch, "ReturnedAudioPlayObject");
}
// creates/returns a dynamic Audio object to position and plays in the world with loop
public static GameObject CreateGetFadeAudioObject(AudioClip aClip, bool dLoop, AnimationClip fadeClip, AnimationClip fadePitch, string objName)
{
// instance a new gameobject
GameObject apObject = new GameObject(objName);
// position the object in the world
apObject.transform.position = Vector3.zero;
// add our DontDestroyOnLoad
DontDestroyOnLoad(apObject);
// add an AudioSource component
apObject.AddComponent<AudioSource>();
// return this script for use
AudioSource apAudio = apObject.GetComponent<AudioSource>();
// initialize some AudioSource fields
apAudio.playOnAwake = false;
apAudio.rolloffMode = AudioRolloffMode.Linear;
apAudio.loop = dLoop;
apAudio.clip = aClip;
apAudio.volume = 1.0f; // default
if (fadeClip != null)
{
Animation apAnim = apObject.AddComponent<Animation>();
apAnim.AddClip(fadeClip, fadeClip.name);
apAnim.AddClip(fadePitch, fadePitch.name);
apAnim.clip = fadeClip;
apAnim.playAutomatically = false;
}
// return our AudioObject
return apObject;
}
Notice how we have added a new parameter “AnimationClip fadePitch” to pass through the methods, so now you can add a fading pitch animation clip to your animation component of your audio game object.
The final piece is very similar to FadeAudioObject, except we will add a new method in AudioHelper called: PitchAudioPbject like so:
// fade our AudioSource object based on speed (> 0 fades volume up, < 0 fades volume out, == 0 assumes the sound is playing and just destroys it)
public static IEnumerator PitchAudioObject(GameObject aObject, float pitchSpeed)
{
Animation apAnim = aObject.GetComponent<Animation>();
AudioSource aSource = aObject.GetComponent<AudioSource>();
// we are not a fade audio object
if (apAnim == null)
{
// we simply destroy the object and return
if (pitchSpeed <= 0)
{
Destroy (aObject);
}
// we are a positive playing sound, so just play it
if (pitchSpeed > 0 && aSource != null)
{
aSource.Play ();
}
return true;
}
// animation clip is default to pitch out (1 to 2), these will look reveresed but they are correct
if (pitchSpeed < 0)
{
apAnim[apAnim.clip[1].name].time = apAnim[apAnim.clip[1].name].length;
}
else
{
apAnim[apAnim.clip[1].name].time = 0;
}
// set our speed
apAnim[apAnim.clip[1].name].speed = pitchSpeed;
// play the audio
if (aSource.isPlaying == false)
{
aSource.Play();
}
// play the fade
apAnim.Play();
// yield the length of the clip
if (fadeSpeed < 0)
{
while(apAnim.isPlaying) { yield return new WaitForEndOfFrame(); }
Destroy (aObject);
}
}
So basically when you add the to AudioHelper, you would call it similar to fading an audio object, except like so:
StartCoroutine (AudioHelper.PitchAudioObject (BaseManager.globalGameMusic, 0.05f));
This should give a very small upwards change in pitch over time.
For question #3: You can add a couple other methods to AudioHelper that will manage turning on and off all audio volume like so:
{
int cnt = 0;
foreach (var at in GameObject.FindObjectsOfType(typeof(AudioSource)) as AudioSource[])
{
at.volume = (audioVolumes.Count > 0) ? audioVolumes[cnt] : 1.0f;
cnt++;
}
}
public static void DisableAllAudio()
{
audioVolumes = new List<float>();
foreach (var at in GameObject.FindObjectsOfType(typeof(AudioSource)) as AudioSource[])
{
audioVolumes.Add(at.volume);
at.volume = 0.0f;
}
}
When your ready to mute your volume, simply call: AudioHelper.DisableAllAudio(); and when you want to turn it back on, call: AudioHelper.EnableAllAudio();
For question #4: You can contra the speed by changing the -0.25 to a different number, for example to fade out faster, change the -0.25f to -0.5f, or even -1, to fade volume up, change it to a positive higher number like: 0.75f
Sorry for th elate reply, and I hope that helps you out.
-Raiden