您的位置:首页 > 移动开发 > Unity3D

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

2016-10-11 20:57 701 查看
制作一个简单的声音控制器,采取参处窗口的形式来制作,弹出窗口被做成预设体,点击设置的时候加载,再次点击时销毁内存中的预设体克隆对象;

使用单例来控制弹出窗口实例的唯一性。







代码:

//控制声音的单例类
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;
}
}
}

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