Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
UI_VolumeSlider.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
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.0001f)
{
slider.value = _value;
}
}
}
UI.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UI : MonoBehaviour,ISaveManager
{
[Header("End screen")]
[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_itemTooltip itemToolTip;
public UI_statToolTip statToopTip;
public Ui_SkillToolTip skillToolTip;
public UI_CraftWindow craftWindow;
[SerializeField] private UI_VolumeSlider[] volumeSettings;
public void Awake()
{
SwitchTo(skillTreeUI);//修复可能出现skill没法加载成功的bug
}
public void Start()
{
SwitchTo(inGameUI);
itemToolTip.gameObject.SetActive(false);
statToopTip.gameObject.SetActive(false);
}
private 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)//切换窗口函数
{
for (int i = 0; i < transform.childCount; i++)
{
bool fadeScreen = transform.GetChild(i).GetComponent<UI_FadeScreen>() != null;//保证存在淡入淡出效果的函数时才会为真,才会使darkScreen保持存在
if (!fadeScreen)
transform.GetChild(i).gameObject.SetActive(false);
}
if (_menu != null)
{
_menu.SetActive(true);
}
}
public void SwitchWithKeyTo(GameObject _menu)//键盘切换窗口函数
{
if (_menu != null && _menu.activeSelf)//通过判断是否传入mune和mune是否激活来决定使设置为可视或不可使
{
_menu.SetActive(false);
CheckForInGameUI();
return;
}
SwitchTo(_menu);
}
private void CheckForInGameUI()//当其他UI不在时自动切换值InGameUI函数
{
for (int i = 0; i < transform.childCount; i++)
{
if (transform.GetChild(i).gameObject.activeSelf && transform.GetChild(i).GetComponent<UI_FadeScreen>() == null) //修复InGameUI在fadeScreen打开后,没法存在的问题
return;
}
SwitchTo(inGameUI);
}
public void SwitchOnEndScreen()//死亡综合效果函数
{
SwitchTo(null);
fadeScreen.FadeOut();
StartCoroutine(EndScreenCorutine());
}
IEnumerator EndScreenCorutine()//死亡显示文本函数
{
yield return new WaitForSeconds(1);
endText.SetActive(true);
yield return new WaitForSeconds(1.5f);
restartButton.SetActive(true);
}
public void RestartGameButton()//场景重开函数
{
GameManager.instance.RestratScene();//调用GameManager的重开函数
}
public void LoadData(GameData _data)
{
foreach(KeyValuePair<string,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);
}
}
}
GameData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GameData
{
public int currency;
public SerializableDictionary<string, bool> skillTree;
public SerializableDictionary<string, int> inventory;
public List<string> equipmentId;
public SerializableDictionary<string, bool> checkpoints;
public string closestCheckpointId;
public float lostCurrencyX;
public float lostCurrencyY;
public int lostCurrencyAmount;
public SerializableDictionary<string, float> volumeSettings;
public GameData()
{
this.lostCurrencyX = 0;
this.lostCurrencyY = 0;
this.lostCurrencyAmount = 0;
this.currency = 0;
skillTree = new SerializableDictionary<string, bool>();
inventory = new SerializableDictionary<string, int>();
equipmentId = new List<string>();
closestCheckpointId = string.Empty;
checkpoints = new SerializableDictionary<string, bool>();
volumeSettings = new SerializableDictionary<string, float>();
}
}