딕셔너리를 사용하여 필요한 효과음의 이름을 매개변수로 받아서 실행하도록 하였다.
하이라키창에 배열로 드래그 앤 드롭으로 하지않고 Awake에서 Resources.Load로 받아보았다.
public class AudioManager : MonoBehaviour
{
// 게임 내 오디오를 관리
// BGM / SFX 등을 재생하거나 중지
// 오디오 리소스 로드 / 해제 담당
// 필요한 효과음이 있다면 주석으로 적어놔 주세요. 보고 추가해두겠습니다.
private AudioSource _backgroundMusic;
private AudioSource _soundEffects;
private Dictionary<string, AudioClip> sfx = new Dictionary<string, AudioClip>();
private Dictionary<string, AudioClip> bgm = new Dictionary<string, AudioClip>();
public void InitalizeAudios()
{
#region AudioClip
sfx.Add("bossLongATK", Resources.Load<AudioClip>("Sound/SoundEffect/BossLongDistanceATK"));
sfx.Add("bossShortATK", Resources.Load<AudioClip>("Sound/SoundEffect/BossShortDistanceATK"));
sfx.Add("monsterAxeATK", Resources.Load<AudioClip>("Sound/SoundEffect/MonsterAxeATK"));
sfx.Add("monsterFireATK", Resources.Load<AudioClip>("Sound/SoundEffect/MonsterFireATK"));
sfx.Add("monsterSwordATK", Resources.Load<AudioClip>("Sound/SoundEffect/MonsterSwordATK"));
sfx.Add("monsterUpperATK", Resources.Load<AudioClip>("Sound/SoundEffect/MonsterUpperATK"));
sfx.Add("monsterHit", Resources.Load<AudioClip>("Sound/SoundEffect/MonsterHit"));
sfx.Add("playerHit", Resources.Load<AudioClip>("Sound/SoundEffect/PlayerHit"));
sfx.Add("playerNormalATK", Resources.Load<AudioClip>("Sound/SoundEffect/PlayerNormalATK"));
sfx.Add("buttonClick", Resources.Load<AudioClip>("Sound/SoundEffect/ButtonClick"));
sfx.Add("gameClear", Resources.Load<AudioClip>("Sound/SoundEffect/GameClear"));
sfx.Add("jump", Resources.Load<AudioClip>("Sound/SoundEffect/Jump"));
sfx.Add("potal", Resources.Load<AudioClip>("Sound/SoundEffect/Potal"));
bgm.Add("normalRoomBGM1", Resources.Load<AudioClip>("Sound/Music/Fractal"));
bgm.Add("normalRoomBGM2", Resources.Load<AudioClip>("Sound/Music/Frozy"));
bgm.Add("bossRoomBGM", Resources.Load<AudioClip>("Sound/Music/Jumptowin"));
bgm.Add("shopRoomBGM", Resources.Load<AudioClip>("Sound/Music/Stars"));
bgm.Add("startSceneBGM", Resources.Load<AudioClip>("Sound/Music/Nightwind"));
#endregion
if (_backgroundMusic == null && _soundEffects == null)
{
_backgroundMusic = GetComponent<AudioSource>();
_soundEffects = GetComponent<AudioSource>();
}
_backgroundMusic.loop = true;
_backgroundMusic.volume = 0.2f;
_soundEffects.volume = 0.3f;
_soundEffects.playOnAwake = false;
}
public void SFX(string name)
{
_soundEffects.PlayOneShot(sfx[name]);
}
public void BGM(string name)
{
_backgroundMusic.Stop();
_backgroundMusic.clip = bgm[name];
_backgroundMusic.Play();
}
}
참고자료 : ![AudioManager]https://qiita.com/mino4273/items/598326fb4b2f818313b4
원래는 아래처럼 코드를 짯지만 너무 비효율적이여서 위에처럼 바꿧다.
[Header("AudioClip")]
[SerializeField]
private AudioClip[] audioClips;
[SerializeField]
private string[] soundName;
// SFX
// Monster
private AudioClip bossLongATK;
private AudioClip bossShortATK;
private AudioClip monsterAxeATK;
private AudioClip monsterFireATK;
private AudioClip monsterSwordATK;
private AudioClip monsterUpperATK;
private AudioClip monsterHit;
// Player
private AudioClip playerHit;
private AudioClip playerNormalATK;
// Besides
private AudioClip buttonClick;
private AudioClip gameClear;
private AudioClip jump;
private AudioClip potal; // sdsdsdsds
// BGM
private AudioClip normalRoomBGM1;
private AudioClip normalRoomBGM2;
private AudioClip bossRoomBGM;
private AudioClip shopRoomBGM;
private AudioClip startSceneBGM;
// SFX
public void BossLongATK() => _soundEffects.PlayOneShot(bossLongATK);
public void BossShortATK() => _soundEffects.PlayOneShot(bossShortATK);
public void MonsterAxeATK() => _soundEffects.PlayOneShot(monsterAxeATK);
public void MonsterFireATK() => _soundEffects.PlayOneShot(monsterFireATK);
public void MonsterSwordATK() => _soundEffects.PlayOneShot(monsterSwordATK);
public void MonsterUpperATK() => _soundEffects.PlayOneShot(monsterUpperATK);
public void MonsterHit() => _soundEffects.PlayOneShot(monsterHit);
public void PlayerHit() => _soundEffects.PlayOneShot(playerHit);
public void PlayerNormalATK() => _soundEffects.PlayOneShot(playerNormalATK);
public void ButtonClick() => _soundEffects.PlayOneShot(buttonClick);
public void GameClear() => _soundEffects.PlayOneShot(gameClear);
public void Jump() => _soundEffects.PlayOneShot(jump);
// BGM
public void NormalRoomBGM1() => _backgroundMusic.clip = normalRoomBGM1;
public void NormalRoomBGM2() => _backgroundMusic.clip = normalRoomBGM2;
public void BossRoomBGM() => _backgroundMusic.clip = bossRoomBGM;
public void ShopRoomBGM() => _backgroundMusic.clip = shopRoomBGM;
public void StartSceneBGM() => _backgroundMusic.clip = startSceneBGM;
public void BGMStart() => _backgroundMusic.Play();
public void BGMStop() => _backgroundMusic.Stop();
UIManamger 자식으로 있는 StatsWindow 오브젝트(스탯창)의 스크립트이다.
사진처럼 누른다고 바로 올라가는게 아닌 들어가는 비용과 올라가는 스탯을 보여주고 다시 내리거나 취소가 가능하고 적용 버튼을 눌러야 적용이 되도록 하였다.
public class StatsWindows : MonoBehaviour
{
[Header("Stats InterFace")]
public TextMeshProUGUI Level_stats;
public TextMeshProUGUI HP_stats;
public TextMeshProUGUI power_stats;
public TextMeshProUGUI mobility_stats;
public TextMeshProUGUI attackSpeed_stats;
[Header("Stats Modifier")]
public TextMeshProUGUI Gem_modifier;
public TextMeshProUGUI HP_modifier;
public TextMeshProUGUI power_modifier;
public TextMeshProUGUI mobility_modifier;
public TextMeshProUGUI attackSpeed_modifier;
private int Gem_modifierCount = 0;
private float hp_modifierCount = 0;
private float power_modifierCount = 0;
private float mobility_modifierCount = 0.0f;
private float attackSpeed_modifierCount = 0;
[HideInInspector]
private float power;
private float mobility = 0.5f;
private float attackSpeed;
private int Level = 1;
private int maxHealth;
DataManager dataManager;
private int statsUpPrice = 50;
private void Awake()
{
dataManager = GameManager.instance.DataManager;
UpdatePlayerStats();
// Test
GameManager.instance.UpdatePlayerCurrentGolds(1000);
//Stats 설정에 값들이 보이게 설정
UpdateStatsWindow();
}
private void OnEnable()
{
if (dataManager != null)
{
Gem_modifierCount = dataManager.PlayerCurrentGold;
UpdateStatsWindow();
}
}
#region Stat Text Update
private void UpdatePlayerStats()
{
maxHealth = dataManager.PlayerCurrentStats.maxHealth;
power = dataManager.PlayerCurrentStats.attackSO.power;
attackSpeed = dataManager.PlayerCurrentStats.attackSO.attackSpeed;
mobility = dataManager.PlayerCurrentStats.speed;
}
private void UpdateStatsWindow()
{
Level_stats.text = Level.ToString();
HP_stats.text = maxHealth.ToString() + $"+({hp_modifierCount * 4})";
power_stats.text = power.ToString() + $"+({power_modifierCount * 3})";
mobility_stats.text = mobility.ToString();
attackSpeed_stats.text = attackSpeed.ToString();
Gem_modifier.text = Gem_modifierCount.ToString();
}
private void UpdateStatsModifier()
{
HP_modifier.text = hp_modifierCount.ToString();
power_modifier.text = power_modifierCount.ToString();
mobility_modifier.text = mobility_modifierCount.ToString();
attackSpeed_modifier.text = attackSpeed_modifierCount.ToString();
Gem_modifier.text = Gem_modifierCount.ToString();
}
#endregion
#region Gem Check
private bool StatsUpGoldCheck()
{
if (Gem_modifierCount <= statsUpPrice)
return false;
Gem_modifierCount -= statsUpPrice;
statsUpPrice += 10;
return true;
}
private void StatsDownGoldCheck()
{
statsUpPrice -= 10;
Gem_modifierCount += statsUpPrice;
}
#endregion
#region ResetModifier
private void ResetModifierCount()
{
hp_modifierCount = 0;
power_modifierCount = 0;
mobility_modifierCount = 0;
attackSpeed_modifierCount = 0;
}
#endregion
#region Stats Apply&Cancel Button
public void StatsApplyButton()
{
GameManager.instance.UpdatePlayerAttackSODatas(attackSpeed_modifierCount * 0.9f, power_modifierCount * 3, 0);
GameManager.instance.UpdatePlayerStatsDatas((int)hp_modifierCount * 4, (int)hp_modifierCount * 4, mobility_modifierCount * 0.1f);
GameManager.instance.UpdatePlayerCurrentGolds(-(dataManager.PlayerCurrentGold - Gem_modifierCount));
ResetModifierCount();
UpdatePlayerStats();
UpdateStatsWindow();
UpdateStatsModifier();
}
public void StatsCancelButton()
{
ResetModifierCount();
UpdatePlayerStats();
UpdateStatsWindow();
UpdateStatsModifier();
}
#endregion
#region Player Stats Up&Down Button
// 리팩토링 예정입니다.
public void ModifierCountUp(int index)
{
if (!StatsUpGoldCheck()) return;
switch (index)
{
case 0:
hp_modifierCount++;
break;
case 1:
power_modifierCount++;
break;
case 2:
mobility_modifierCount++;
break;
case 3:
attackSpeed_modifierCount++;
break;
}
UpdateStatsModifier();
UpdateStatsWindow();
}
public void ModifierCountDown(int index)
{
switch (index)
{
case 0:
if (hp_modifierCount <= 0) return;
StatsDownGoldCheck();
hp_modifierCount--;
break;
case 1:
if (power_modifierCount <= 0) return;
StatsDownGoldCheck();
power_modifierCount--;
break;
case 2:
if (mobility_modifierCount <= 0) return;
StatsDownGoldCheck();
mobility_modifierCount--;
break;
case 3:
if (attackSpeed_modifierCount <= 0) return;
StatsDownGoldCheck();
attackSpeed_modifierCount--;
break;
}
UpdateStatsModifier();
UpdateStatsWindow();
}
#endregion
}