VirtualTeaching_log_AudioControl

//全局声音设置

为实现全局声音,在所有场景之前就创建一个用户到达不了的场景,有GameObje设为  Bgmusic

创建脚本,控制该声音组件在场景切换的时候不被摧毁:

 void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
        Application.LoadLevel("Sc001");<span style="white-space:pre">			</span>//这里在程序Awake时就跳转场景
    }

//为了管理声音,创建一个专门管理声音的单例

public class SoundManager
{
    private static SoundManager instance;           //单例
    private SoundManager() { }                      //将构造函数设为私有,禁止在外面构造

    public static SoundManager GetInstance()
    {
        if(instance == null)
        {
            instance=new SoundManager();
        }
        return instance;
    }

    //全局声音大小 默认值为0.5;
    public float _soundVolum = .5f;

}

//调整场景声音大小

获取声音管理单例之后,将声音滑块的值与单例中的_soundVolum对应起来。

即这里的_soundVolum对应的就是全局声音大小。

创建脚本 控制声音大小:

    #region 变量
    public AudioSource _bgmusic;                        //背景声音
    public bool _playSoundOrNot = false;                //是否播放
    GameObject audioContainer;                          //临时存放声音GameObject的容器
        
    SoundManager sm;                                    //声音管理单例
    public float _soundVolum = 0;                       //用来接收声音管理单例中控制声音大小的量

    #endregion
   
	// Use this for initialization
	void Start () {

        //获取背景声音
        audioContainer = GameObject.FindGameObjectWithTag("BackMusic");
        _bgmusic = audioContainer.gameObject.GetComponent<AudioSource>();
        
        //声音管理单例
        sm = SoundManager.GetInstance();

        //判断是否播放声音
        Debug.Log("是否播放声音:"+_playSoundOrNot);
        if (_bgmusic != null)
        {
            if (_playSoundOrNot)
            {
                if (_bgmusic.isPlaying)
                {
                    Debug.Log("the audio is playing");
                    return;
                }
                _bgmusic.Play();
            }
            else
                _bgmusic.Stop();
        }
        else {
            Debug.Log("The audio is null!");
        }

	}
	
	// Update is called once per frame
	void Update () {
       //在每帧中都把声音控制单例中的声音大小 赋值给当前的游戏背景声音
        if (_bgmusic != null)
        {
            _bgmusic.volume = sm._soundVolum;
        }
        
	}

























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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值