Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
AreaSound.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AreaSound : MonoBehaviour
{
[SerializeField] private int areaSoundIndex;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.GetComponent<Player>() != null)
{
AudioManager.instance.PlaySFX(areaSoundIndex, null);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if(collision.GetComponent<Player>()!=null)
{
AudioManager.instance.StopSFXWithTime(areaSoundIndex);//退出区域后,声音缓慢减少
}
}
}
AudioManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public static AudioManager instance;
[SerializeField] private float sfxMinimumDistance;
[SerializeField] private AudioSource[] sfx;
[SerializeField] private AudioSource[] bgm;
public bool playBgm;
private int bgmIndex;
private bool canPlaySFX;//
private void Awake()
{
if (instance != null)
{
Destroy(instance.gameObject);
}
else
instance = this;
Invoke("AllowSFX", 1f);//让游戏延迟一秒后才能发出声效
}
private void Update()
{
if (!playBgm)
StopAllBGM();
else
{
if (!bgm[bgmIndex].isPlaying)
PlayBGM(bgmIndex);
}
}
public void PlaySFX(int _sfxIndex,Transform _source)
{
//if (sfx[_sfxIndex].isPlaying)//防止出现多个相同物体同时发出声音
//{
// return;
//}
if(canPlaySFX == false)
{
return;
}
if (_source != null && Vector2.Distance(PlayerManager.instance.player.transform.position, _source.position) > sfxMinimumDistance)//防止东西很远的情况下发出声音
return;
if(_sfxIndex < sfx.Length)
{
sfx[_sfxIndex].pitch = Random.Range(.85f, 1.1f);
sfx[_sfxIndex].Play();
}
}
public void StopSFX(int _sfxIndex)
{
sfx[_sfxIndex].Stop();
}
public void PlayRandomBGM()
{
bgmIndex = Random.Range(0, bgm.Length);
PlayBGM(bgmIndex);
}
public void PlayBGM(int _bgmIndex)
{
bgmIndex = _bgmIndex;
StopAllBGM();
if (_bgmIndex < sfx.Length)
{
bgm[_bgmIndex].Play();
}
}
public void StopAllBGM()
{
for(int i = 0; i < bgm.Length; i++)
{
bgm[i].Stop();
}
}
private void AllowSFX()
{
canPlaySFX = true;
}
public void StopSFXWithTime(int _index)
{
StartCoroutine(DecreaseVolume(sfx[_index]));
}
private IEnumerator DecreaseVolume(AudioSource _audio)//退出区域后,声音缓慢减少
{
float defaultVolume = _audio.volume;
while (_audio.volume > .1f)
{
_audio.volume -= _audio.volume * .2f;
yield return new WaitForSeconds(.6f);
if (_audio.volume <= .1f)
{
_audio.Stop();
_audio.volume = defaultVolume;
break;
}
}
}
}
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)
{
AudioManager.instance.PlaySFX(7,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);
}
}
}