unity3d,声音播放

1、audioclip

把音频文件导入到unity以后,音频文件就会变成一个audioclip。


点击,可以看到属性

unity3d,声音播放_第1张图片

load type:小文件选择Decompress On Load,大的文件选Compressed In Memory

Compression Format:PCM不压缩,一般选ADPCM,背景音乐,对话啥的选Vorbis/MP3

Sample Rate Setting:一般选Optimize Sample Rate,优化采样率


上面的是根据官方文档得出的结论,不过好像实际运用中还得看具体情况。


2、AudioSource

unity3d,声音播放_第2张图片

这是unity播放声音的基本组件,关联了audioclip就能够播放了。声音播放基本是在对这个组件进行操作。


3、AudioListener

每个场景只能有一个的组件,声音播放必须的组件,默认绑在main camera上。


4、示例

unity3d,声音播放_第3张图片

unity3d,声音播放_第4张图片

点击鼠标左键,生成一个立方体,同时发出声音。(模拟子弹射击,玩家发招的声音情况)

这个声音经常使用,就绑定在控制方

using UnityEngine;
using System.Collections;

public class ThrowCube : MonoBehaviour {

	public GameObject perfab;
	public AudioClip shoot_sound;
	private AudioSource audio_source;

	void Start () {
		audio_source = GetComponent<AudioSource> ();
	}

	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonDown (0)) {
			audio_source.PlayOneShot (shoot_sound,GameInfo.effect_sound_volume);

			GameObject shoot_obj = Instantiate (perfab, transform.position, transform.rotation) as GameObject;
			shoot_obj.GetComponent<Rigidbody> ().AddRelativeForce (new Vector3 (Input.mousePosition.x/10, Input.mousePosition.y/5, 50f));
		}
	}
}


立方体掉出屏幕,发出声音并销毁。(模拟怪物被击中的情况)

有种做法,因为这个声音使用频度也可能很高,就把声音绑着控制方,如玩家上。但是,这边为了编程省事,把声音绑着怪物上。

稍微麻烦的地方是要等声音播放完毕再销毁。

using UnityEngine;
using System.Collections;

public class CubeControl : MonoBehaviour
{

	public AudioClip destory_sound;
	private AudioSource audio_source;

	void Awake ()
	{
		audio_source = GetComponent<AudioSource> ();
		audio_source.mute = GameInfo.effect_sound_mute;
	}

	void OnBecameInvisible ()
	{
		audio_source.PlayOneShot (destory_sound,GameInfo.effect_sound_volume);
		InvokeRepeating ("DestorySelf", 0, .1f);
	}

	void DestorySelf(){
		if (!audio_source.isPlaying) {
			Destroy (gameObject);
		}
	}
}


场景开始播放背景音乐

点击鼠标右键更换播放的背景音乐(模拟事件触发背景音改变)

using UnityEngine;
using System.Collections;

public class BgSoundControl : MonoBehaviour
{

	public AudioClip[] bg_sounds;
	private AudioSource audio_source;

	void Awake ()
	{
		audio_source = GetComponent<AudioSource> ();
		audio_source.volume = GameInfo.bg_music_volume;
		audio_source.clip = bg_sounds [0];
	}
	
	// Update is called once per frame
	void Update ()
	{
		if (Input.GetMouseButtonDown (1)) {
			audio_source.clip = bg_sounds [1];
			audio_source.Play ();
		}
	}
}


添加音量控制

为了方便控制,把背景音乐的游戏对象方到指定的tag便于控制。

建立一个静态类存放音量信息,同时便于多方调用

单次播放的声音,在播放的时候,传入音量大小的参数控制音量,背景声音是控制audiosource的volume值控制音量

public static class GameInfo {

	public static float effect_sound_volume;
	public static float bg_music_volume;

	public static bool effect_sound_mute;
	public static bool bg_music_mute;

}


音量设置的类

using UnityEngine;
using System.Collections;
using UnityEngine.UI;




public class SoundSetControl : MonoBehaviour
{
	private AudioSource bg_music;
	public Slider sound_slider;
	public Slider music_slider;
	public Toggle sound_toggle;
	public Toggle music_toggle;
	public Button confirm_button;
	public Button exit_button;
	public AudioClip sound_clip;
	public AudioClip music_clip;
	private AudioSource audio_source;
	public delegate void SoundSetAction();
	public event SoundSetAction OnClose;


	// Use this for initialization
	void Awake ()
	{
		audio_source = GetComponent<AudioSource> ();
		AudioSource[] list = FindObjectsOfType<AudioSource> ();
		for (int i = 0; i < list.Length; i++) {
			if (list [i].gameObject.tag == "BackgroundMusic") {
				bg_music = list [i];
				break;
			}
		}
		LoadSoundSet ();
		SetUI ();
		Confirm ();
		gameObject.SetActive (false);
	}

	void OnEnable (){
		GetComponent<AudioSource> ().mute = false;
	}
		
	public void SetEffectSound ()
	{
		audio_source.PlayOneShot (sound_clip, sound_slider.value);
		sound_toggle.isOn = false;
	}

	public void SetBgMusic ()
	{
		audio_source.PlayOneShot (music_clip, music_slider.value);
		music_toggle.isOn = false;
	}

	//根据变量设置游戏音量
	public void Confirm(){
		GameInfo.bg_music_mute = music_toggle.isOn;
		GameInfo.effect_sound_mute = sound_toggle.isOn;
		GameInfo.bg_music_volume = music_slider.value;
		GameInfo.effect_sound_volume = sound_slider.value;

		Save ();

		AudioSource[] list = FindObjectsOfType<AudioSource> ();
		for (int i = 0; i < list.Length; i++) {
			list [i].mute = GameInfo.effect_sound_mute;
		}
		bg_music.mute = GameInfo.bg_music_mute;
		bg_music.volume = GameInfo.bg_music_volume;
	}

	public void Exit(){
		//SoundSetAction ();
		if (OnClose != null) {
			OnClose ();
		}
		gameObject.SetActive (false);
	}
		
	private void LoadSoundSet ()
	{
		if (PlayerPrefs.GetInt ("SoundMute", 1)==1) {
			GameInfo.effect_sound_mute = true;
		} else {
			GameInfo.effect_sound_mute = false;
		}

		if (PlayerPrefs.GetInt ("BgMusicMute", 1)==1) {
			GameInfo.bg_music_mute = true;
		} else {
			GameInfo.bg_music_mute = false;
		}

		GameInfo.effect_sound_volume = PlayerPrefs.GetFloat ("EffectSoundVolume", 1f);
		GameInfo.bg_music_volume = PlayerPrefs.GetFloat ("BgMusicVolume", 1f);
	}

	private void SetUI(){
		sound_slider.value = GameInfo.effect_sound_volume;
		sound_toggle.isOn = GameInfo.effect_sound_mute;
		music_slider.value = GameInfo.bg_music_volume;
		music_toggle.isOn = GameInfo.bg_music_mute;
	}

	private void Save(){
		if (GameInfo.effect_sound_mute) {
			PlayerPrefs.SetInt ("SoundMute", 1);
		} else {
			PlayerPrefs.SetInt ("SoundMute", 0);
		}

		if (GameInfo.bg_music_mute) {
			PlayerPrefs.SetInt ("BgMusicMute", 1);
		} else {
			PlayerPrefs.SetInt ("BgMusicMute",0);
		}

		PlayerPrefs.SetFloat ("EffectSoundVolume", GameInfo.effect_sound_volume);
		PlayerPrefs.SetFloat ("BgMusicVolume", GameInfo.bg_music_volume);
	}
}


关闭的时候,用了事件,这样做的想法是,把这个类绑在canvas上,整个canvas可以做成预制件,方便在多个场景中使用。

事件交由统一的事件处理类,将事件交到其他类去执行。目的是解耦合。

using UnityEngine;
using System.Collections;

public class EventManage : MonoBehaviour {

	private GameManage gameManage;
	private SoundSetControl soundSetControl;

	void Awake(){
		gameManage = FindObjectOfType<GameManage> ();
		soundSetControl = FindObjectOfType<SoundSetControl> ();
	}
		

	// Use this for initialization
	void Start () {
		soundSetControl.OnClose+=gameManage.GamePlay;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GameManage : MonoBehaviour {

	//public delegate void GameAction();
	//public event GameAction OnGamePause,OnGamePlay;

	public GameObject sound_canvas;

	private ThrowCube throw_cube;
	private AudioSource bg_music;

	public Text txt;

	// Use this for initialization
	void Awake () {
		throw_cube = FindObjectOfType<ThrowCube> ();

		AudioSource[] list = FindObjectsOfType<AudioSource> ();
		for (int i = 0; i < list.Length; i++) {
			if (list [i].gameObject.tag == "BackgroundMusic") {
				bg_music = list [i];
				break;
			}
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey (KeyCode.Escape)) {
			sound_canvas.SetActive (true);
			GamePause ();
		}

		txt.text = GameInfo.bg_music_mute.ToString () + ";" + GameInfo.bg_music_volume.ToString () + ";" + GameInfo.effect_sound_mute.ToString () + ";" + GameInfo.effect_sound_volume.ToString ();
	}

	public void GamePause(){
		throw_cube.enabled = false;
		bg_music.Pause ();
	}

	public void GamePlay(){
		throw_cube.enabled = true;
		bg_music.Play ();
	}
}


内容下载:http://download.csdn.net/detail/wuyt2008/9474143




你可能感兴趣的:(unity3d,audio)