다양한 기록

[Fortress Craft] Sound, FxSound 본문

유니티 엔진/Fortress Craft

[Fortress Craft] Sound, FxSound

라구넹 2025. 2. 6. 02:36

효과음은 애셋 쓰고, 배경음은 AI로 만들었음

 

1. FxSound

public void Attack()  
{
	if (attackInputTimer.Expired(Runner) && animState.fullPathHash != animAttack )
	{
		if (Job == JobType.Archer)
		{
			archerFire.FireDirection = lastDir;
			archerFire.SetDamageByLevel(level, Job);
			
			if (!BuffAttackTimer.Expired(Runner))
			{
				archerFire.BuffDamage(coefAttack);
			}
			
			Invoke("PlaySound1", 0.4f);
		}
		else if (Job == JobType.Magician)
		{
			magicianFire.SetDamageByLevel(level, Job);

			if( !BuffAttackTimer.Expired(Runner) )
            {
				magicianFire.BuffDamage(coefAttack);
            }

			Invoke("PlaySound1", 0.3f);
		}
		else if( Job == JobType.Warrior)
		{
			SetAttack(level, Job);
			wairrorWeapon.Damage = AttackDamage;

			if( !BuffAttackTimer.Expired(Runner) )
            {
				wairrorWeapon.Damage *= coefAttack;
            }

			Invoke("PlaySound1", 0.2f);
		}
		_netAnimator.Animator.SetTrigger("Attack");
		attackInputTimer = TickTimer.CreateFromSeconds(Runner, 0.2f);
	}
}

public void PlaySound1()
{
    sound1.volume = SoundManager.Instance.SFXVolume;
    sound1.Play();
}

이벤트()에 Sound를 달아줌

그리고 틀 때는 SoundManager에서 소리를 가져옴

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Agit.FortressCraft
{
    public class SoundManager : MonoBehaviour
    {
        [SerializeField] private GameObject BGM_Main;

        public static SoundManager Instance { get; set; }

        public float BGMVolume { get; set; }
        public float SFXVolume { get; set; }

        private void Awake()
        {
            Instance = this;

            BGMVolume = 1.0f;
            SFXVolume = 1.0f;
        }

        public void UpdateBGMVolume()
        {
            BGM bgm = GameObject.Find("BGM").GetComponent<BGM>();
            bgm.SetVolume(BGMVolume);
        }
    }
}

SoundManager

 

2. BGM

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Agit.FortressCraft
{
    public class BGM : MonoBehaviour
    {
        private AudioSource bgm = null;

        private void Awake()
        {
            bgm = GetComponent<AudioSource>();
        }

        private void Start()
        {
            bgm.volume = SoundManager.Instance.BGMVolume;
        }

        public void SetVolume(float volume)
        {
            if (bgm == null) return;
            bgm.volume = volume;
        }
    }
}

Photon 특징: 씬 이동해도 이전 씬 안없어짐

이번 프로젝트에선 씬 두개가 동시에 유지되는 형태임 (맨 처음 씬이 계속 유지됨)

=> 그냥 배경 음악 넣어두고 씬 넘어가면 알아서 삭제되겠지 불가능

 

public void Start()  // UI Changer Call
{
	currentSceneName = gameObject.name;

	if (gameObject.name == "Lobby" || gameObject.name == "TeamLobby")
	{
		if (FindObjectOfType<App>().mode == Mode.Team){
			_setTeamGroup.SetActive(true);
        }
		else
		{
            _singleModeMap.SetActive(true);
        }
        if (gameObject.name == "TeamLobby")
		{

		}
		Instantiate(BGM_Lobby).gameObject.name = "BGM";
	}

	if (gameObject.name == "Battle")
	{
		FindObjectOfType<UIManager>().LoadingMsg.SetActive(false);


        GameObject bgm = FindObjectOfType<BGM>().gameObject;
        if (bgm != null)
            Destroy(FindObjectOfType<BGM>().gameObject);

        Instantiate(BGM_Battle).gameObject.name = "BGM";

        FindObjectOfType<LevelUIController>().BattleSceneUIChange();

		Invoke("SpawnCastle", 2f);
    }
    if (gameObject.name == "RPG")
	{
        FindObjectOfType<LevelUIController>().RPGSceneUIChange();
    }
}

맵에 따라 BGM 삭제 및 생성이 필요 함

 

그리고 BGM 업데이트 시 UpdateVolume Call 하도록 슬라이더에 연결