六一的部落格


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



Mute Sound


说明

绑定键位, 实现静音

可能不止一个地方会用到音量调节, 创建音效接口库

实现两个接口:

  1. 设置音量大小
  2. 打开或关闭声音

API导航


USoundClass::Properties

可配置属性

1/** Configurable properties like volume and priority. */
2UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = General, meta = (ShowOnlyInnerProperties))
3FSoundClassProperties Properties;

音量

 1/**
 2 * Structure containing configurable properties of a sound class.
 3 */
 4PRAGMA_DISABLE_DEPRECATION_WARNINGS
 5USTRUCT(BlueprintType)
 6struct FSoundClassProperties
 7{
 8    GENERATED_USTRUCT_BODY()
 9
10    /** Volume multiplier. */
11    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = General)
12    float Volume;
13
14    // ...
15};

USoundClass

派生自UObject, UObject派生自UObjectBaseUtility


UObjectBaseUtility::GetName

获取描述

1/**
2  * Returns the name of this object (with no path information)
3  * 
4  * @return Name of the object.
5  */
6 FORCEINLINE FString GetName() const
7 {
8     return GetFName().ToString();
9 }

创建音效接口库

-
基类 BlueprintFunctionLibrary
名称 STUSoundFuncLib
路径 Sound
属性 Public

添加头文件搜索路径

ShootThemUp: ShootThemUp.Build.cs

 1PublicIncludePaths.AddRange(new string[]
 2{
 3    "ShootThemUp/Public/Player",
 4    "ShootThemUp/Public/Components",
 5    "ShootThemUp/Public/Dev",
 6    "ShootThemUp/Public/Weapon",
 7    "ShootThemUp/Public/UI",
 8    "ShootThemUp/Public/Animations",
 9    "ShootThemUp/Public/Pickups",
10    "ShootThemUp/Public/Weapon/Components",
11    "ShootThemUp/Public/AI",
12    "ShootThemUp/Public/AI/Tasks",
13    "ShootThemUp/Public/AI/Services",
14    "ShootThemUp/Public/AI/EQS",
15    "ShootThemUp/Public/AI/Decorators",
16    "ShootThemUp/Public/Menu",
17    "ShootThemUp/Public/Menu/UI",
18    "ShootThemUp/Public/Sound"       
19});

添加接口: 设置音量大小

public

ShootThemUp: Sound/STUSoundFuncLib.h

1class USoundClass;
2
3UFUNCTION(BlueprintCallable)
4static void SetSoundClassVolume(USoundClass *SoundClass, float Volume);

ShootThemUp: Sound/STUSoundFuncLib.cpp

 1#include "Sound/SoundClass.h"
 2
 3DEFINE_LOG_CATEGORY_STATIC(LogSTUSoundFuncLib, All, All);
 4
 5void USTUSoundFuncLib::SetSoundClassVolume(USoundClass *SoundClass, float Volume)
 6{
 7    if (!SoundClass) return;
 8
 9    SoundClass->Properties.Volume = FMath::Clamp(Volume, 0.0f, 1.0f);
10
11    UE_LOG(LogSTUSoundFuncLib, Display, TEXT("Sound class volume was changed: %s = %f"), *SoundClass->GetName(),
12           SoundClass->Properties.Volume);
13}

添加接口: 打开或关闭声音

public

ShootThemUp: Sound/STUSoundFuncLib.h

1UFUNCTION(BlueprintCallable)
2static void ToggleSoundClassVolume(USoundClass *SoundClass);

ShootThemUp: Sound/STUSoundFuncLib.cpp

1void USTUSoundFuncLib::ToggleSoundClassVolume(USoundClass *SoundClass)
2{
3    if (!SoundClass) return;
4
5    const auto NextVolume = SoundClass->Properties.Volume > 0.0f ? 0.0f : 1.0f;
6    SetSoundClassVolume(SoundClass, NextVolume);
7}

创建Sound Class资产

Content/Sounds/Settings

当前有5个Sound Class资产:

  • SC_Character
  • SC_UI
  • SC_Weapon
  • SC_Impact
  • SC_Pickup

挨个调节很麻烦

  1. 创建Sound Class资产SC_Master, 将5个Sound Class资产作为其子节点


  2. 调整子节点: SC_Weapon作为SC_Character的子节点, SC_Impact作为SC_Weapon的子节点


  3. 如此组织后, 无法单独打开5个Sound Class资产之一, 而是打开SC_Master

  4. 调节SC_Master时, 其子节点都会受到影响; 调节SC_Weapon时, 其子节点均会受到影响


测试

BP_STUPlayerController

  1. 按下 1 时, 打开关闭声音


  2. 按下 2 时, 轮流调大和调小武器和击中音效


    STUSoundFuncLib


    Flip Flop


  3. 日志

    • 按下 1 : 静音
    • 再次按下 1 : 恢复音量
    • 按下 2 : 调小武器和击中音量
    • 再次按下 2 : 调大武器和击中音量



键位绑定: 按下M静音/恢复音量

项目设置 > Input



实现键位绑定

  1. 在游戏实例配置Sound Class

    ShootThemUp: STUGameInstance.h

    1class USoundClass;
    2
    3// public
    4void ToggleVolume();
    5
    6// protected
    7UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
    8USoundClass *SoundClass; /*MasterSoundClass*/

    ShootThemUp: STUGameInstance.cpp

    1#include "Sound/STUSoundFuncLib.h"
    2
    3void USTUGameInstance::ToggleVolume()
    4{
    5    USTUSoundFuncLib::ToggleSoundClassVolume(SoundClass);
    6}
  2. 在玩家控制器中绑定键位

    private

    ShootThemUp: Player/STUPlayerController.h

    1void Mute();

    ShootThemUp: Player/STUPlayerController.cpp

    1// SetupInputComponent
    2InputComponent->BindAction("Mute", IE_Pressed, this, &ASTUPlayerController::Mute);
     1#include "STUGameInstance.h"
     2
     3void ASTUPlayerController::Mute()
     4{
     5    if (!GetWorld()) return;
     6
     7    const auto GameInstance = GetWorld()->GetGameInstance<USTUGameInstance>();
     8
     9    if (GameInstance)
    10    {
    11        GameInstance->ToggleVolume();
    12    }
    13}

查看

  1. 设置Sound Class

    BP_STUGameInstance


  2. 设置项目默认Sound Class

    项目设置 > Engine > Audio


静音


Mute Sound


说明

绑定键位, 实现静音

可能不止一个地方会用到音量调节, 创建音效接口库

实现两个接口:

  1. 设置音量大小
  2. 打开或关闭声音

API导航


USoundClass::Properties

可配置属性

1/** Configurable properties like volume and priority. */
2UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = General, meta = (ShowOnlyInnerProperties))
3FSoundClassProperties Properties;

音量

 1/**
 2 * Structure containing configurable properties of a sound class.
 3 */
 4PRAGMA_DISABLE_DEPRECATION_WARNINGS
 5USTRUCT(BlueprintType)
 6struct FSoundClassProperties
 7{
 8    GENERATED_USTRUCT_BODY()
 9
10    /** Volume multiplier. */
11    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = General)
12    float Volume;
13
14    // ...
15};

USoundClass

派生自UObject, UObject派生自UObjectBaseUtility


UObjectBaseUtility::GetName

获取描述

1/**
2  * Returns the name of this object (with no path information)
3  * 
4  * @return Name of the object.
5  */
6 FORCEINLINE FString GetName() const
7 {
8     return GetFName().ToString();
9 }

创建音效接口库

-
基类 BlueprintFunctionLibrary
名称 STUSoundFuncLib
路径 Sound
属性 Public

添加头文件搜索路径

ShootThemUp: ShootThemUp.Build.cs

 1PublicIncludePaths.AddRange(new string[]
 2{
 3    "ShootThemUp/Public/Player",
 4    "ShootThemUp/Public/Components",
 5    "ShootThemUp/Public/Dev",
 6    "ShootThemUp/Public/Weapon",
 7    "ShootThemUp/Public/UI",
 8    "ShootThemUp/Public/Animations",
 9    "ShootThemUp/Public/Pickups",
10    "ShootThemUp/Public/Weapon/Components",
11    "ShootThemUp/Public/AI",
12    "ShootThemUp/Public/AI/Tasks",
13    "ShootThemUp/Public/AI/Services",
14    "ShootThemUp/Public/AI/EQS",
15    "ShootThemUp/Public/AI/Decorators",
16    "ShootThemUp/Public/Menu",
17    "ShootThemUp/Public/Menu/UI",
18    "ShootThemUp/Public/Sound"       
19});

添加接口: 设置音量大小

public

ShootThemUp: Sound/STUSoundFuncLib.h

1class USoundClass;
2
3UFUNCTION(BlueprintCallable)
4static void SetSoundClassVolume(USoundClass *SoundClass, float Volume);

ShootThemUp: Sound/STUSoundFuncLib.cpp

 1#include "Sound/SoundClass.h"
 2
 3DEFINE_LOG_CATEGORY_STATIC(LogSTUSoundFuncLib, All, All);
 4
 5void USTUSoundFuncLib::SetSoundClassVolume(USoundClass *SoundClass, float Volume)
 6{
 7    if (!SoundClass) return;
 8
 9    SoundClass->Properties.Volume = FMath::Clamp(Volume, 0.0f, 1.0f);
10
11    UE_LOG(LogSTUSoundFuncLib, Display, TEXT("Sound class volume was changed: %s = %f"), *SoundClass->GetName(),
12           SoundClass->Properties.Volume);
13}

添加接口: 打开或关闭声音

public

ShootThemUp: Sound/STUSoundFuncLib.h

1UFUNCTION(BlueprintCallable)
2static void ToggleSoundClassVolume(USoundClass *SoundClass);

ShootThemUp: Sound/STUSoundFuncLib.cpp

1void USTUSoundFuncLib::ToggleSoundClassVolume(USoundClass *SoundClass)
2{
3    if (!SoundClass) return;
4
5    const auto NextVolume = SoundClass->Properties.Volume > 0.0f ? 0.0f : 1.0f;
6    SetSoundClassVolume(SoundClass, NextVolume);
7}

创建Sound Class资产

Content/Sounds/Settings

当前有5个Sound Class资产:

  • SC_Character
  • SC_UI
  • SC_Weapon
  • SC_Impact
  • SC_Pickup

挨个调节很麻烦

  1. 创建Sound Class资产SC_Master, 将5个Sound Class资产作为其子节点


  2. 调整子节点: SC_Weapon作为SC_Character的子节点, SC_Impact作为SC_Weapon的子节点


  3. 如此组织后, 无法单独打开5个Sound Class资产之一, 而是打开SC_Master

  4. 调节SC_Master时, 其子节点都会受到影响; 调节SC_Weapon时, 其子节点均会受到影响


测试

BP_STUPlayerController

  1. 按下 1 时, 打开关闭声音


  2. 按下 2 时, 轮流调大和调小武器和击中音效


    STUSoundFuncLib


    Flip Flop


  3. 日志

    • 按下 1 : 静音
    • 再次按下 1 : 恢复音量
    • 按下 2 : 调小武器和击中音量
    • 再次按下 2 : 调大武器和击中音量



键位绑定: 按下M静音/恢复音量

项目设置 > Input



实现键位绑定

  1. 在游戏实例配置Sound Class

    ShootThemUp: STUGameInstance.h

    1class USoundClass;
    2
    3// public
    4void ToggleVolume();
    5
    6// protected
    7UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
    8USoundClass *SoundClass; /*MasterSoundClass*/

    ShootThemUp: STUGameInstance.cpp

    1#include "Sound/STUSoundFuncLib.h"
    2
    3void USTUGameInstance::ToggleVolume()
    4{
    5    USTUSoundFuncLib::ToggleSoundClassVolume(SoundClass);
    6}
  2. 在玩家控制器中绑定键位

    private

    ShootThemUp: Player/STUPlayerController.h

    1void Mute();

    ShootThemUp: Player/STUPlayerController.cpp

    1// SetupInputComponent
    2InputComponent->BindAction("Mute", IE_Pressed, this, &ASTUPlayerController::Mute);
     1#include "STUGameInstance.h"
     2
     3void ASTUPlayerController::Mute()
     4{
     5    if (!GetWorld()) return;
     6
     7    const auto GameInstance = GetWorld()->GetGameInstance<USTUGameInstance>();
     8
     9    if (GameInstance)
    10    {
    11        GameInstance->ToggleVolume();
    12    }
    13}

查看

  1. 设置Sound Class

    BP_STUGameInstance


  2. 设置项目默认Sound Class

    项目设置 > Engine > Audio