原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1226/277.html
Unity3D使用NGUI制作简易弹出窗口声音控制器
这里常州手游开发-幻天网络要和大家分享下使用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();
}
}
//挂载在手机App外包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;
}
}
}
}
上篇:上一篇:常州网游开发-U3D Scroll View
下篇:下一篇:常州移动游戏开发-U3D纹理旋转两种实现