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;
}
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值