SoundSubsystem - osy9611/ProjectPT GitHub Wiki

Unity์˜ SoundManager ๊ฐ„๋‹จ ์ •๋ฆฌ

  • Unity์—์„œ๋Š” SoundManager๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ์‚ฌ์šด๋“œ ์žฌ์ƒ ๋ฐ ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ ์ž‘์„ฑํ–ˆ๋‹ค.

  • ์ค‘์•™์—์„œ ์‚ฌ์šด๋“œ ์ œ์–ด๋ฅผ ํ†ตํ•ด ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์šฉ์ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ–ˆ๋‹ค.

  • ์ผ๋ถ€ ์ฝ”๋“œ

    public void Play(AudioClip clip, eSound type = eSound.FX, float pitch = 1.0f)
    {
        if (clip == null)
            return;
    
        if (type == eSound.Bgm)
        {
            AudioSource audioSource = audioSources[(int)eSound.Bgm];
            if (audioSource.isPlaying)
                audioSource.Stop();
    
            audioSource.pitch = pitch;
            audioSource.clip = clip;
            audioSource.Play();
        }
        else
        {
            AudioSource audioSource = audioSources[(int)type];
            audioSource.pitch = pitch;
            audioSource.PlayOneShot(clip);
        }
    }

Unreal์—์„œ ๊ตฌํ˜„ํ•œ SoundSubsystem

  • Unreal์—์„œ๋„ SoundSubsystem์„ ๊ตฌํ˜„ํ•ด์„œ ํ•˜๋‚˜์˜ Subsystem์—์„œ ์‚ฌ์šด๋“œ๋ฅผ ์ œ์–ดํ–ˆ๋‹ค.

  • Unity SoundManager์—์„œ๋Š” ๊ฐ ํƒ€์ž…์— ๋”ฐ๋ผ ์‚ฌ์šด๋“œ๋ฅผ ์žฌ์ƒํ•˜์˜€๋‹ค. ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ๋„ ์ด์™€ ๋™์ผํ•˜๊ฒŒ ๊ตฌํ˜„์„ ์ง„ํ–‰ํ•จ

    • BGM : ๊ฒŒ์ž„์˜ ๋ฐฐ๊ฒฝ์Œ์„ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•œ ํƒ€์ž…
    • SFX : ์‚ฌ์šด๋“œ ํŠน์ˆ˜ ํšจ๊ณผ๋ฅผ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•œ ํƒ€์ž…
    • Voice : ์บ๋ฆญํ„ฐ ๋ชฉ์†Œ๋ฆฌ ์‚ฌ์šด๋“œ๋ฅผ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•œ ํƒ€์ž…
  • Unreal์—์„œ๋Š” ๊ฐ ์”ฌ์ด ์ „ํ™˜๋˜๋ฉด AudioComponent์— ์ „ํ™˜๋œ World๋ฅผ ๋“ฑ๋ก ํ•ด์ค˜์•ผํ•œ๋‹ค.

    • Unreal์€ World ๋‹จ์œ„๋กœ ์˜ค๋””์˜ค ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ด€๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ์”ฌ์œผ๋กœ ๋„˜์–ด๊ฐˆ๋•Œ AudioComponent์— World๋ฅผ ์žฌ ๋“ฑ๋กํ•ด์ฃผ๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.

      void UPTAudioSubsystem::RegisterData()
      {
      	int32 MaxCount = static_cast<int32>(ESoundType::Max);
      	for (int32 i = 0; i < MaxCount; ++i)
      	{
      		ESoundType SoundType = static_cast<ESoundType>(i);
      		UAudioComponent* AudioComponent = GetAudioComponent(SoundType);
      		if (!AudioComponent)
      		{
      			AudioComponent = NewObject<UAudioComponent>(this);
      			AudioComponents.Add(SoundType, AudioComponent);
      		}
      
      		if (SoundType == ESoundType::BGM)
      		{
      			AudioComponent->bAutoActivate = true;
      		}
      		AudioComponent->RegisterComponentWithWorld(GetWorld());
      		//AudioComponent->RegisterComponent();
      	}
      }
      void UPTAudioSubsystem::UnRegisterData()
      {
      	int32 MaxCount = static_cast<int32>(ESoundType::Max);
      
      	for (int32 i = 0; i < MaxCount; ++i)
      	{
      		ESoundType SoundType = static_cast<ESoundType>(i);
      		UAudioComponent* AudioComponent = GetAudioComponent(SoundType);
      		if (!AudioComponent)
      		{
      			continue;
      		}
      		AudioComponent->UnregisterComponent();
      	}
      }
  • ๊ฐ ์‚ฌ์šด๋“œ ํƒ€์ž…์— ๋”ฐ๋ผ AssetManager์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ๋น„๋™๊ธฐ๋กœ ๋กœ๋“œํ•ด์„œ 2D, 3D ์‚ฌ์šด๋“œ๋ฅผ ์žฌ์ƒํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„

    void UPTAudioSubsystem::PlaySound3D_ByPath(ESoundType Type, const FSoftObjectPath& BGMPath, FVector Location)
    {
    	if (UAudioComponent* AudioComponent = GetAudioComponent(Type))
    	{
    		UPTAssetManager& AssetManager = UPTAssetManager::Get();
    		AssetManager.AsynchronusLoadAsset(BGMPath, [&, AudioComponent](UObject* result)
    			{
    				if (USoundBase* Sound = Cast<USoundBase>(result))
    				{
    					AudioComponent->SetSound(Sound);
    					AudioComponent->SetWorldLocation(Location);
    					AudioComponent->Play();
    				}
    			});
    	}
    }
    
    void UPTAudioSubsystem::PlaySound2D_ByPath(ESoundType Type, const FSoftObjectPath& Path)
    {
    	if (UAudioComponent* AudioComponent = GetAudioComponent(Type))
    	{
    		UPTAssetManager& AssetManager = UPTAssetManager::Get();
    		AssetManager.AsynchronusLoadAsset(Path, [&, AudioComponent](UObject* result)
    			{
    				if (USoundBase* Sound = Cast<USoundBase>(result))
    				{
    					AudioComponent->SetSound(Sound);
    					AudioComponent->Play();
    				}
    			});
    	}
    }
โš ๏ธ **GitHub.com Fallback** โš ๏ธ