SaaS网站可以做seo嘛,网站平台建设规划,wordpress 301规则,山东省住房建设部网站【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
教程源地址#xff1a;https://www.udemy.com/course/2d-rpg-alexdev/ 本章节实现了音频的大小设置与保存加载 音频管理器
UI_VolumeSlider.cs
定义了 UI_VolumeSlider 类#xff0c;用于处理与音频设置相关的…【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
教程源地址https://www.udemy.com/course/2d-rpg-alexdev/ 本章节实现了音频的大小设置与保存加载 音频管理器
UI_VolumeSlider.cs
定义了 UI_VolumeSlider 类用于处理与音频设置相关的 UI 滑块Slider交互。 音量调整 SliderValue(float _value) 方法根据滑块的值调整音量利用 audioMixer.SetFloat 设置音频混音器中指定参数parametr的值。使用 Mathf.Log10(_value) * multiplier 将滑块值转化为对数刻度以平滑音量变化。 加载保存的音量设置 LoadSlider(float _value) 方法将加载的音量值如果值大于 0.001应用到滑块上避免音量翻转。 成员变量 slider关联的 UI 滑块。parametr音频混音器中的参数名称。audioMixer用于音频混合的 AudioMixer 实例。multiplier用于音量计算的乘数。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;//2024.12.2 from summer to winter
public class UI_VolumeSlider : MonoBehaviour
{public Slider slider;//滑块public string parametr;//音频混合器的参数[SerializeField] private AudioMixer audioMixer;//音频混合器[SerializeField] private float multiplier;//音量的倍数public void SliderValue(float _value) audioMixer.SetFloat(parametr, Mathf.Log10(_value) * multiplier);//设置音量public void LoadSlider(float _value)//加载音量{if(_value 0.001f)//防止翻转slider.value _value;}}UI.cs
添加的部分
这段代码包含了两个方法LoadData 和 SaveData用于加载和保存游戏中的音量设置。它们的主要功能是处理音量设置的保存和加载机制将音量设置保存在 GameData 对象中并能够恢复。
1. LoadData(GameData _data):
功能加载音量设置并应用到 UI 滑块。逻辑 遍历传入的 GameData 对象中的 volumeSettings 字典字典存储了音量设置的参数名和相应的值如 masterVolume, musicVolume 等。对每一个音量设置项KeyValuePairstring, float遍历 volumeSettingsUI 控件中的 UI_VolumeSlider 实例检查每个 UI_VolumeSlider 的 parametr音频参数名称是否与保存的音量设置的参数名称相匹配。如果匹配就调用 UI_VolumeSlider 的 LoadSlider 方法将保存的音量值应用到相应的 UI 滑块上。
2. SaveData(ref GameData _data):
功能保存当前的音量设置到 GameData 中。逻辑 清空 GameData 对象中的 volumeSettings 字典以确保旧的音量设置不被遗留。遍历 volumeSettingsUI 控件中的 UI_VolumeSlider 实例获取每个滑块的值并将其添加到 volumeSettings 字典中。每个滑块的 parametr音频参数名称作为字典的键slider.value滑块当前的值作为字典的值保存到 GameData 中。 public void LoadData(GameData _data){foreach(KeyValuePairstring,float pair in _data.volumeSettings)//遍历音量设置{foreach (UI_VolumeSlider item in volumeSettings){if (item.parametr pair.Key)//如果音量设置的参数和保存的参数一致item.LoadSlider(pair.Value);}}}public void SaveData(ref GameData _data){_data.volumeSettings.Clear();//清空音量设置foreach (UI_VolumeSlider item in volumeSettings)//遍历音量设置{_data.volumeSettings.Add(item.parametr,item.slider.value);//保存音量设置}}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class UI : MonoBehaviour,ISaveManager
{[Header(End screens)][SerializeField] private UI_FadeScreen fadeScreen;[SerializeField] private GameObject endText;[SerializeField] private GameObject restartButton;[Space][SerializeField] private GameObject characterUI;[SerializeField] private GameObject skillTreeUI;[SerializeField] private GameObject craftUI;[SerializeField] private GameObject optionsUI;[SerializeField] private GameObject inGameUI;//物品提示框和状态提示框public UI_SkillToolTip skillToolTip;public UI_ItemTooltip itemToolTip;public UI_StatToolTip statToolTip;public UI_CraftWindow craftWindow;[SerializeField] private UI_VolumeSlider[] volumeSettings;private void Awake(){SwitchTo(skillTreeUI);//2024年11月22日P138 Skill Tree Hot Fix启动时默认显示技能树界面fadeScreen.gameObject.SetActive(true);}void Start(){SwitchTo(inGameUI);itemToolTip.gameObject.SetActive(false);//戏启动时隐藏物品提示框和状态提示框statToolTip.gameObject.SetActive(false);}void Update(){if (Input.GetKeyDown(KeyCode.C))SwitchWithKeyTo(characterUI);if (Input.GetKeyDown(KeyCode.B))SwitchWithKeyTo(craftUI);if (Input.GetKeyDown(KeyCode.K))SwitchWithKeyTo(skillTreeUI);if (Input.GetKeyDown(KeyCode.O))SwitchWithKeyTo(optionsUI);}public void SwitchTo(GameObject _menu)// 该方法用于切换到指定的UI界面{for (int i 0; i transform.childCount; i)//遍历当前UI对象的所有子物体{bool fadeScreen transform.GetChild(i).GetComponentUI_FadeScreen() ! null;//检查UI界面是否有FadeScreensif (fadeScreenfalse)transform.GetChild(i).gameObject.SetActive(false);//遍历并隐藏所有子元素,确保了在显示新的UI界面时所有其他的UI界面都会被隐藏}if (_menu ! null)//传入的菜单不为空{_menu.SetActive(true);//显示}}public void SwitchWithKeyTo(GameObject _menu)//处理切换UI的逻辑{if (_menu ! null _menu.activeSelf)// UI界面已经显示隐藏, 如果目标UI界面未显示调用 SwitchTo 显示。{_menu.SetActive(false);CheckForInGameUI();return;}SwitchTo(_menu);}private void CheckForInGameUI()//关闭其他UI都会回到InGameUI{for (int i 0; i transform.childCount; i)//{//p152修复切换到其他UI时InGameUI关闭的问题if (transform.GetChild(i).gameObject.activeSelf transform.GetChild(i).GetComponentUI_FadeScreen() null)//其他的UI全部关闭并且淡入淡出关闭return;}SwitchTo(inGameUI);}public void SwitchOnEndScreen(){fadeScreen.FadeOut();StartCoroutine(EndScreenCorutione());}IEnumerator EndScreenCorutione(){yield return new WaitForSeconds(1);endText.SetActive(true);yield return new WaitForSeconds(1.7f);restartButton.SetActive(true);}public void RestartGameButton() GameManager.instance.RestartScene();public void LoadData(GameData _data){foreach(KeyValuePairstring,float pair in _data.volumeSettings)//遍历音量设置{foreach (UI_VolumeSlider item in volumeSettings){if (item.parametr pair.Key)//如果音量设置的参数和保存的参数一致item.LoadSlider(pair.Value);}}}public void SaveData(ref GameData _data){_data.volumeSettings.Clear();//清空音量设置foreach (UI_VolumeSlider item in volumeSettings)//遍历音量设置{_data.volumeSettings.Add(item.parametr,item.slider.value);//保存音量设置}}
}
文章转载自: http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.dbxss.cn.gov.cn.dbxss.cn http://www.morning.cwjsz.cn.gov.cn.cwjsz.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.gstg.cn.gov.cn.gstg.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.fhbhr.cn.gov.cn.fhbhr.cn http://www.morning.rwnx.cn.gov.cn.rwnx.cn http://www.morning.ggrzk.cn.gov.cn.ggrzk.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.lztrt.cn.gov.cn.lztrt.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.qmzhy.cn.gov.cn.qmzhy.cn http://www.morning.yqlrq.cn.gov.cn.yqlrq.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.bpmnl.cn.gov.cn.bpmnl.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.hsrch.cn.gov.cn.hsrch.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.xykst.cn.gov.cn.xykst.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.rgsgk.cn.gov.cn.rgsgk.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.yfddl.cn.gov.cn.yfddl.cn http://www.morning.ktfnj.cn.gov.cn.ktfnj.cn http://www.morning.xywfz.cn.gov.cn.xywfz.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.pghfy.cn.gov.cn.pghfy.cn http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.bxch.cn.gov.cn.bxch.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.wcczg.cn.gov.cn.wcczg.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.rmppf.cn.gov.cn.rmppf.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.nlnmy.cn.gov.cn.nlnmy.cn http://www.morning.qqfcf.cn.gov.cn.qqfcf.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn