Unity中游戏的声音管理

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

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


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

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

/// <summary>
/// 在指定位置播放剪辑
/// </summary>
/// <param name="clip"></param>
/// <param name="emitter"></param>
/// <returns></returns>
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);
}

/// <summary>
/// 创建一个空物体并且附加一个AudioSource组件来播放音效,
/// 并且需要给它传递一个transform给他作为播放的位置,
/// 播放完毕后自动删除
/// </summary>
/// <param name="clip">音频剪辑引用</param>
/// <param name="emitter"></param>
/// <param name="volume"></param>
/// <param name="pitch"></param>
/// <param name="isLoop">是否循环</param>
/// <returns>音频源</returns>
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<AudioSource>();
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;
}

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

}


/// <summary>
/// 指定位置播放剪辑。播放完成后自动消除音频源。
/// 进一步控制播放
/// </summary>
/// <param name="clip">音频剪辑</param>
/// <param name="position">播放音效的3D空间位置</param>
/// <param name="volume">音量大小</param>
/// <returns></returns>
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<AudioSource>();
source.clip = clip;
source.volume = volume;
source.pitch = pitch;
source.Play();
source.loop = isLoop;
if (!isLoop) Destroy(go, clip.length);
return source;
}
}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页