六一的部落格


关关难过关关过,前路漫漫亦灿灿。



Play Sound 2D


说明

使用C++实现:

  1. 进入游戏时播放开始音效
  2. 显示暂停和游戏结束窗口时出场音效

创建Sound Cue资产

ExternalContent/Sounds/UI/wav Content/Sounds/UI
sw_UI_GameStart 游戏开始音效 SCue_GameStart
sw_UI_Open 窗口出场音效 SCue_WidgetOpen
  • wav


  • 创建Sound Cue


  • 移动到Content/Sounds/UI



API导航

  1. 播放Sound Cue

    Sound为空退出
     1/**
     2​ * Plays a sound directly with no attenuation, perfect for UI sounds.
     3 *
     4​ * * Fire and Forget.
     5​ * * Not Replicated.
     6​ * @param Sound - Sound to play.
     7​ * @param VolumeMultiplier - A linear scalar multiplied with the volume, in order to make the sound louder or softer.
     8​ * @param PitchMultiplier - A linear scalar multiplied with the pitch.
     9​ * @param StartTime - How far in to the sound to begin playback at
    10​ * @param ConcurrencySettings - Override concurrency settings package to play sound with
    11​ * @param OwningActor - The actor to use as the "owner" for concurrency settings purposes. 
    12 *						Allows PlaySound calls to do a concurrency limit per owner.
    13​ * @param bIsUISound - True if sound is UI related, else false
    14 */
    15UFUNCTION(BlueprintCallable, BlueprintCosmetic, Category="Audio", meta=( WorldContext="WorldContextObject", AdvancedDisplay = "2", UnsafeDuringActorConstruction = "true" ))
    16static void PlaySound2D(const UObject* WorldContextObject, USoundBase* Sound, float VolumeMultiplier = 1.f, float PitchMultiplier = 1.f, float StartTime = 0.f, USoundConcurrency* ConcurrencySettings = nullptr, const AActor* OwningActor = nullptr, bool bIsUISound = true);
  2. USoundBase

    Sound Cue和Sound Wave的基类
     1/**
     2​ * The behavior of audio playback is defined within Sound Cues.
     3 */
     4UCLASS(hidecategories=object, BlueprintType, meta= (LoadBehavior = "LazyOnDemand"))
     5class ENGINE_API USoundCue : public USoundBase
     6{
     7    // ...
     8};
     9
    10UCLASS(hidecategories=Object, editinlinenew, BlueprintType, meta= (LoadBehavior = "LazyOnDemand"))
    11class ENGINE_API USoundWave : public USoundBase, public IAudioProxyDataFactory, public IInterface_AsyncCompilation
    12{
    13    // ...
    14};

播放游戏开始音效

  1. 添加属性

    protected

    ShootThemUp: Menu/UI/STUMenuWidget.h

    1class USoundCue;
    2
    3UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
    4USoundCue *StartGameSound;
  2. 点击按钮时播放

    ShootThemUp: Menu/UI/STUMenuWidget.cpp

    1#include "Sound/SoundCue.h"
    2
    3// OnStartGame
    4UGameplayStatics::PlaySound2D(GetWorld(), StartGameSound);

窗口部件出场时播放音效


为窗口部件基类实现该功能

  1. 添加属性

    protected

    ShootThemUp: UI/STUBaseWidget.h

    1class USoundCue;
    2
    3UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
    4USoundCue *OpenSound;
  2. 添加接口

    public

    ShootThemUp: UI/STUBaseWidget.h

    1void PlayOpenSound();

    ShootThemUp: UI/STUBaseWidget.cpp

    1#include "Sound/SoundCue.h"
    2#include "Kismet/GameplayStatics.h"
    3
    4void USTUBaseWidget::PlayOpenSound()
    5{
    6    UGameplayStatics::PlaySound2D(GetWorld(), OpenSound);
    7}

窗口部件出场时播放音效

ShootThemUp: UI/STUGameHUD.cpp

1// OnMatchStateChanged
2
3if (CurrentWidget)
4{
5    CurrentWidget->Show();
6    CurrentWidget->PlayOpenSound();
7    CurrentWidget->SetVisibility(ESlateVisibility::Visible);
8}

设置对应Sound Cue资产

  1. 游戏开始


  2. 暂停


  3. 结束


为窗口部件添加音效


Play Sound 2D


说明

使用C++实现:

  1. 进入游戏时播放开始音效
  2. 显示暂停和游戏结束窗口时出场音效

创建Sound Cue资产

ExternalContent/Sounds/UI/wav Content/Sounds/UI
sw_UI_GameStart 游戏开始音效 SCue_GameStart
sw_UI_Open 窗口出场音效 SCue_WidgetOpen
  • wav


  • 创建Sound Cue


  • 移动到Content/Sounds/UI



API导航

  1. 播放Sound Cue

    Sound为空退出
     1/**
     2​ * Plays a sound directly with no attenuation, perfect for UI sounds.
     3 *
     4​ * * Fire and Forget.
     5​ * * Not Replicated.
     6​ * @param Sound - Sound to play.
     7​ * @param VolumeMultiplier - A linear scalar multiplied with the volume, in order to make the sound louder or softer.
     8​ * @param PitchMultiplier - A linear scalar multiplied with the pitch.
     9​ * @param StartTime - How far in to the sound to begin playback at
    10​ * @param ConcurrencySettings - Override concurrency settings package to play sound with
    11​ * @param OwningActor - The actor to use as the "owner" for concurrency settings purposes. 
    12 *						Allows PlaySound calls to do a concurrency limit per owner.
    13​ * @param bIsUISound - True if sound is UI related, else false
    14 */
    15UFUNCTION(BlueprintCallable, BlueprintCosmetic, Category="Audio", meta=( WorldContext="WorldContextObject", AdvancedDisplay = "2", UnsafeDuringActorConstruction = "true" ))
    16static void PlaySound2D(const UObject* WorldContextObject, USoundBase* Sound, float VolumeMultiplier = 1.f, float PitchMultiplier = 1.f, float StartTime = 0.f, USoundConcurrency* ConcurrencySettings = nullptr, const AActor* OwningActor = nullptr, bool bIsUISound = true);
  2. USoundBase

    Sound Cue和Sound Wave的基类
     1/**
     2​ * The behavior of audio playback is defined within Sound Cues.
     3 */
     4UCLASS(hidecategories=object, BlueprintType, meta= (LoadBehavior = "LazyOnDemand"))
     5class ENGINE_API USoundCue : public USoundBase
     6{
     7    // ...
     8};
     9
    10UCLASS(hidecategories=Object, editinlinenew, BlueprintType, meta= (LoadBehavior = "LazyOnDemand"))
    11class ENGINE_API USoundWave : public USoundBase, public IAudioProxyDataFactory, public IInterface_AsyncCompilation
    12{
    13    // ...
    14};

播放游戏开始音效

  1. 添加属性

    protected

    ShootThemUp: Menu/UI/STUMenuWidget.h

    1class USoundCue;
    2
    3UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
    4USoundCue *StartGameSound;
  2. 点击按钮时播放

    ShootThemUp: Menu/UI/STUMenuWidget.cpp

    1#include "Sound/SoundCue.h"
    2
    3// OnStartGame
    4UGameplayStatics::PlaySound2D(GetWorld(), StartGameSound);

窗口部件出场时播放音效


为窗口部件基类实现该功能

  1. 添加属性

    protected

    ShootThemUp: UI/STUBaseWidget.h

    1class USoundCue;
    2
    3UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
    4USoundCue *OpenSound;
  2. 添加接口

    public

    ShootThemUp: UI/STUBaseWidget.h

    1void PlayOpenSound();

    ShootThemUp: UI/STUBaseWidget.cpp

    1#include "Sound/SoundCue.h"
    2#include "Kismet/GameplayStatics.h"
    3
    4void USTUBaseWidget::PlayOpenSound()
    5{
    6    UGameplayStatics::PlaySound2D(GetWorld(), OpenSound);
    7}

窗口部件出场时播放音效

ShootThemUp: UI/STUGameHUD.cpp

1// OnMatchStateChanged
2
3if (CurrentWidget)
4{
5    CurrentWidget->Show();
6    CurrentWidget->PlayOpenSound();
7    CurrentWidget->SetVisibility(ESlateVisibility::Visible);
8}

设置对应Sound Cue资产

  1. 游戏开始


  2. 暂停


  3. 结束