所有场景不间断的播放音乐
2016-06-12 12:01
344 查看
using UnityEngine; using System.Collections; public class beijingyin : MonoBehaviour { static beijingyin _instance; public AudioClip _audio; public AudioSource ads; //音量 public float musicVolume; Texture image0; Texture image1; Texture image2; //public scro // Use this for initialization public static beijingyin instance { get { if (_instance == null) { _instance = FindObjectOfType<beijingyin>(); DontDestroyOnLoad(_instance.gameObject); } return _instance; } } void Awake() { //此脚本永不消毁,并且每次进入初始场景时进行判断,若存在重复的则销毁 if (_instance == null) { _instance = this; DontDestroyOnLoad(this); } else if (this != _instance) { Destroy(gameObject); } } void Start() { StartCoroutine(AudioSourcePlay(_audio)); //设置默认音量 musicVolume = 0.5F; image0 = (Texture)Resources.Load("kai"); image1 = (Texture)Resources.Load("guan"); image2 = (Texture)Resources.Load("ting"); } // Update is called once per frame void Update() { if (Application.loadedLevelName == "Login") { ////_instance = this; print("jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"); Destroy(gameObject); } } public IEnumerator AudioSourcePlay(AudioClip adc) { ads.clip = adc; ads.Play(); while (ads.isPlaying) { yield return new WaitForSeconds(0f); } } void OnGUI() { //播放音乐按钮 if (GUI.Button(new Rect(10, Screen.height-30, 40, 30), image0)) { //没有播放中 if (!ads.isPlaying) { //播放音乐 ads.Play(); } } //关闭音乐按钮 if (GUI.Button(new Rect(55, Screen.height-30, 40, 30), image1)) { if (ads.isPlaying) { //关闭音乐 ads.Stop(); } } //暂停音乐 //if (GUI.Button(new Rect(Screen.width - 110, 300, 40, 30), image2)) //{ // if (ads.isPlaying) // { // //暂停音乐 // //这里说一下音乐暂停以后 // //点击播放音乐为继续播放 // //而停止以后在点击播放音乐 // //则为从新播放 // //这就是暂停与停止的区别 // ads.Pause(); // } // else // { // ads.Play(); // } //} //创建一个横向滑动条用于动态修改音乐音量 //第一个参数 滑动条范围 //第二个参数 初始滑块位置 //第三个参数 起点 //第四个参数 终点 musicVolume = GUI.HorizontalSlider(new Rect(100, Screen.height-30, 60, 50), musicVolume, 0.0F, 1.0F); //将音量的百分比打印出来 GUI.Label(new Rect(100, Screen.height-20, 300, 20), "音量是 " + (int)(musicVolume * 100) + "%"); if (ads.isPlaying) { //音乐播放中设置音乐音量 取值范围 0.0F到 1.0 ads.volume = musicVolume; } } }
相关文章推荐
- linux安全和调优
- enq: TX - row lock contention等待事件
- nodejs中的模块
- 在VS2010下编译libjpeg及其应用例子
- zabbix安装配置
- 2016春节红包话题总结
- 解决android studio not recognizing known sRGB profile that has been edited问题
- net用multipart/form-data协议post上传文件
- Matlab安装
- USB枚举
- Java JavaWeb 定时任务
- Servlet复习
- adb remount
- 手机传感器简述
- Spring 中如何使用外部属性文件
- NoSql的产生
- mysql 区间分组 interval,elt 的运用 【备忘】
- 局域网电脑Sql2008 R2无法连接到localhost 解决方案
- nagios安装配置
- cacti安装