您的位置:首页 > 其它

所有场景不间断的播放音乐

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: