Unity3D 使用NGUI制作简易弹出窗口声音控制器

制作一个简单的声音控制器,采取参处窗口的形式来制作,弹出窗口被做成预设体,点击设置的时候加载,再次点击时销毁内存中的预设体克隆对象;
使用单例来控制弹出窗口实例的唯一性。

点击设置前界面

点击设置后界面

游戏元素
代码:

//控制声音的单例类
using System.Collections;

public class BackgroundMusicMag {

    #region 
    private AudioSource bgm;
    private static AudioSource audio;
    private static BackgroundMusicMag bgmObj;

    private static bool IsPlay;
    //静态函数中只能调用静态数据成员
    #endregion

    private BackgroundMusicMag(){}
    public static BackgroundMusicMag GetInstance() {
        if (bgmObj == null) {
            bgmObj = new BackgroundMusicMag();
            audio = GameObject.Find("musicFile").GetComponent<AudioSource>();
            IsPlay = true;
        }
        return bgmObj;
    }

    public void SetVol(float vol) {
        if(audio != null) {
            audio.volume = vol;
        }
    }
    public float GetVol() {
        return audio.volume;
    }
    public void SetPit(float pit) {
        if (audio != null) {
            audio.pitch = pit;
        }
    }
    public float GetPit() {
        return audio.pitch;
    }
    /* 改变音乐的播放状态 */
    public void ChangeAudioPlayStatus() {
        if (IsPlay) {
            audio.Pause();
            Debug.Log("Pause");
        } else {
            audio.UnPause();
            Debug.Log("UnPause");
        }
        IsPlay = !IsPlay;
    }
}
//控制应用中操作按钮的单例类,用来加载预设体
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

    /**
    * 单例类,创建或者销毁窗体
     */
public class UIWindowMag {

    private static UIWindowMag windowMag;
    private static Dictionary<string, GameObject> windowCache = new Dictionary<string, GameObject>();

    private UIWindowMag() {}

    public static UIWindowMag GetInstance() {
        if (windowMag == null) {
            windowMag = new UIWindowMag();
        }
        return windowMag;
    }

    public GameObject OpenWindow(string windowName) {
        if (windowCache.ContainsKey(windowName)) {
            return windowCache[windowName];
        }
        GameObject windowObject = Resources.Load(windowName) as GameObject;
        GameObject prefabClone = GameObject.Instantiate(windowObject);

        prefabClone.transform.parent = UIRoot.list[0].transform;
        prefabClone.transform.localPosition = Vector3.zero;
        prefabClone.transform.localScale = Vector3.one;
        windowCache.Add(windowName, prefabClone);
        return prefabClone;
    }
    public void CloseWindow(string windowName) {
        if (windowCache.ContainsKey(windowName)) {
            GameObject.Destroy(windowCache[windowName]);
            windowCache.Remove(windowName);
        }
    }

}
//挂载在弹出窗口面板上(预设体)上的脚本
using UnityEngine;
using System.Collections;

public class UISettingBtn : MonoBehaviour {

    private UISlider[] slider;
    private BackgroundMusicMag bgm;
    private UIToggle switchBtn;

    // Use this for initialization
    void Start () {
        UIButton[] btnArr = GetComponentsInChildren<UIButton>();
        UIEventListener.Get(btnArr[0].gameObject).onClick = CallBack1;

        slider = GetComponentsInChildren<UISlider>();

        bgm = BackgroundMusicMag.GetInstance();

        slider[0].value = bgm.GetVol();
        slider[1].value = bgm.GetPit();
        slider[0].onChange.Add(new EventDelegate(this, "CallBack2"));
        slider[1].onChange.Add(new EventDelegate(this, "CallBack3"));

        switchBtn = GetComponentInChildren<UIToggle>();
        UIEventListener.Get(switchBtn.gameObject).onClick = CallBack4;

    }

    void CallBack1(GameObject obj) {
        UIWindowMag.GetInstance().CloseWindow("audioSettingwindow");
    }
    void CallBack2(GameObject obj) {
        bgm.SetVol(slider[0].value);
    }
    void CallBack3(GameObject obj) {
        bgm.SetPit(slider[1].value);
    }
    void CallBack4(GameObject obj) {
        bgm.ChangeAudioPlayStatus();
    }
}
//挂载在OperatorWindow上的脚本,可以扩展其他的操作按钮
using UnityEngine;
using System.Collections;

public class OperationWindow : MonoBehaviour {

    private UIButton[] btn;

    // Use this for initialization
    void Start () {
        btn = GetComponentsInChildren<UIButton>();

        for (int i = 0; i < btn.Length; i++) {
            UIEventListener.Get(btn[i].gameObject).onClick = CallBack;
        }
    }

    void CallBack(GameObject obj) {
        string name = obj.name;
        switch (name) {
        case "SettingBtn" : {
            UIWindowMag.GetInstance().OpenWindow("audioSettingwindow");
            break;
        }
        case "..." : {
            break;
        }
        }
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值