Unity中游戏的声音管理

从来没写过博客,作为一名菜鸟也不知道写些什么。特地偷同事点料/

源地址:http://www.cnblogs.com/jenke/p/4608422.html


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// 
/// 用来管理音频的各种播放方式
/// --------------修改-----------------
/// 修改播放音量会根据gamedata里面的设置进行变化
/// 
public class AudioManager : Singleton
{

/// 
/// 在指定位置播放剪辑。播放完成后自动消除音频源。
/// 
/// 音频剪辑
/// 播放音效的3D空间位置
/// 音量大小
public void PlayAudioClip(AudioClip clip, Vector3 position, float volume)
{
AudioSource.PlayClipAtPoint(clip, position, volume);
}

/// 
/// 在指定位置播放剪辑
/// 
/// 
/// 
/// 
public AudioSource PlayAudioClip(AudioClip clip)
{
//Debug.Log(this.name + "音量大小为" + 1f * GameData.instance.GetGameSettingData().gameVolum / 100);
return PlayAudioClip(clip, Camera.mainCamera.transform, 1f * GameData.instance.GetGameSettingData().gameVolum / 100, 1f, false);

}

public AudioSource PlayAudioClip(AudioClip clip, bool isLoop)
{
//Debug.Log(this.name + "音量大小为" + 1f * GameData.instance.GetGameSettingData().gameVolum / 100);
return PlayAudioClip(clip, Camera.mainCamera.transform, 1f * GameData.instance.GetGameSettingData().gameVolum / 100, 1f, isLoop);
}

public AudioSource PlayAudioClip(AudioClip clip, Transform emitter)
{
//Debug.Log(this.name + "音量大小为" + 1f * GameData.instance.GetGameSettingData().gameVolum / 100);
return PlayAudioClip(clip, emitter, 1f * GameData.instance.GetGameSettingData().gameVolum / 100, 1f, false);
}

public AudioSource PlayAudioClip(AudioClip clip, Transform emitter, float volume)
{
//Debug.Log(this.name + "音量大小为" + 1f * GameData.instance.GetGameSettingData().gameVolum / 100);
return PlayAudioClip(clip, emitter, 1f * GameData.instance.GetGameSettingData().gameVolum / 100, 1f, false);
}


public AudioSource PlayAudioClip(AudioClip clip, Transform emitter, float volume, float pitch)
{
//Debug.Log(this.name + "音量大小为" + 1f * GameData.instance.GetGameSettingData().gameVolum / 100);
return PlayAudioClip(clip, emitter, 1f * GameData.instance.GetGameSettingData().gameVolum / 100, pitch, false);
}

/// 
/// 创建一个空物体并且附加一个AudioSource组件来播放音效,
/// 并且需要给它传递一个transform给他作为播放的位置,
/// 播放完毕后自动删除
/// 
/// 音频剪辑引用
/// 
/// 
/// 
/// 是否循环
/// 音频源
public AudioSource PlayAudioClip(AudioClip clip, Transform emitter, float volume, float pitch, bool isLoop)
{
//Create an empty game object
GameObject go = new GameObject("Audio: " + clip.name);
go.transform.position = emitter.position;
go.transform.parent = emitter;

//Create the source
AudioSource source = go.AddComponent();
source.clip = clip;
source.volume = 1f * GameData.instance.GetGameSettingData().waitVolum / 100;
source.pitch = pitch;
source.loop = isLoop;
source.Play();
if (!isLoop) Destroy(go, clip.length);
return source;
}

/// 
/// 停止播放当前剪辑,并且删除gameObject
/// 
/// 
public void StopAudioClip(AudioSource audioSource)
{
if (audioSource == null) return;
audioSource.Stop();
audioSource.clip = null;
//Debug.Log("删除" + audioSource.gameObject.name);
Destroy(audioSource.gameObject);

}


/// 
/// 指定位置播放剪辑。播放完成后自动消除音频源。
/// 进一步控制播放
/// 
/// 音频剪辑
/// 播放音效的3D空间位置
/// 音量大小
/// 
public AudioSource PlayAudioClip(AudioClip clip, Vector3 position, float volume, float pitch, bool isLoop)
{
//Create an empty game object
GameObject go = new GameObject("Audio: " + clip.name);
go.transform.position = position;

AudioSource source = go.AddComponent();
source.clip = clip;
source.volume = volume;
source.pitch = pitch;
source.Play();
source.loop = isLoop;
if (!isLoop) Destroy(go, clip.length);
return source;
}
}


你可能感兴趣的:(Unity3D)