标签:
大清早的头疼的不行不行的了,压根就没法好好上班啊啊啊啊啊,偷会闲
准备一个mp3文件,放在Resources目录下
1,Compoent/Create Empty 命名为Audio
2,选中Audio,Compoent/Audio/Audio Source
3,新建脚本Audio_TestDemo.cs
1 public class Audio_TestDemo : MonoBehaviour 2 { 3 4 public AudioSource audioClip; 5 public GameObject player; 6 // Use this for initialization 7 void Start() 8 { 9 audioClip.priority = 0; 10 audioClip.clip = (AudioClip)Resources.Load("Audio_Demo/1", typeof(AudioClip)); ; 11 audioClip.Play(); 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 if (Input.GetKeyUp(KeyCode.A)) 18 { 19 //静音和播放 20 audioClip.mute = !audioClip.mute; 21 } 22 if (Input.GetKey(KeyCode.LeftArrow)) 23 { 24 //改变音调值,加速音频剪辑的播放 25 audioClip.pitch += 0.1f; 26 } 27 if (Input.GetKey(KeyCode.RightArrow)) 28 { 29 //改变音调值,减速音频剪辑的播放 30 audioClip.pitch -= 0.1f; 31 } 32 if (Input.GetKey(KeyCode.UpArrow)) 33 { 34 //音量增大 35 audioClip.volume += 0.1f; 36 } 37 if (Input.GetKey(KeyCode.DownArrow)) 38 { 39 //音量减小 40 audioClip.volume -= 0.1f; 41 } 42 if (Vector3.Distance(player.transform.position, audioClip.transform.position) > audioClip.maxDistance) 43 { 44 //线性衰减,离声源越远声音越小,离声源越近声音越大 45 audioClip.rolloffMode = AudioRolloffMode.Linear; 46 //对数衰减,接近声源时音量大,当远离时音量下降明显 47 //audioClip.rolloffMode = AudioRolloffMode.Logarithmic; 48 // audioClip.rolloffMode = AudioRolloffMode.Custom; 49 } 50 } 51 }
然后将脚本的公共变量赋值就行了
运行音乐就播放啦啦啦
标签:
原文地址:http://www.cnblogs.com/cathytong/p/4682380.html