六一的部落格


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



Quit Game


说明

在主菜单右上角添加按钮, 点击则退出游戏


KismetSystemLibrary

退出游戏

1/** 
2 *	Exit the current game 
3 * @param	SpecificPlayer	The specific player to quit the game. If not specified, player 0 will quit.
4 * @param	QuitPreference	Form of quitting.
5 * @param	bIgnorePlatformRestrictions	Ignores and best-practices based on platform (e.g on some consoles, games should never quit). Non-shipping only
6 */
7UFUNCTION(BlueprintCallable, Category="Game",meta=(WorldContext="WorldContextObject", CallableWithoutWorldContext))
8static void QuitGame(const UObject* WorldContextObject, class APlayerController* SpecificPlayer, TEnumAsByte<EQuitPreference::Type> QuitPreference, bool bIgnorePlatformRestrictions);

QuitPreference

-
Quit 退出
Background 后台运行
 1UENUM()
 2namespace EQuitPreference
 3{
 4    enum Type
 5    {
 6        /** Exit the game completely. */
 7        Quit,
 8        /** Move the application to the background. */
 9        Background,
10    };
11}

在STUMenuWidget实现退出逻辑

  1. 添加属性: 按钮

    protected

    ShootThemUp: Menu/UI/STUMenuWidget.h

    1UPROPERTY(meta = (BindWidget))
    2UButton *QuitGameButton;
  2. 实现回调函数

    private

    ShootThemUp: Menu/UI/STUMenuWidget.h

    1UFUNCTION()
    2void OnQuitGame();

    ShootThemUp: Menu/UI/STUMenuWidget.cpp

    1#include "Kismet/KismetSystemLibrary.h"
    2
    3void USTUMenuWidget::OnQuitGame()
    4{
    5    UKismetSystemLibrary::QuitGame(this, GetOwningPlayer(), EQuitPreference::Quit, true);
    6}
  3. 在NativeOnInitialized注册回调函数

    ShootThemUp: Menu/UI/STUMenuWidget.cpp

    1// NativeOnInitialized
    2
    3if (QuitGameButton)
    4{
    5    QuitGameButton->OnClicked.AddDynamic(this, &USTUMenuWidget::OnQuitGame);
    6}

为WBP_Menu添加退出按钮

  1. 拷贝按钮


  2. 粘贴为画布下级


  3. 设置大小盒

    右上角


    -
    Position X 往右为正
    Position Y 往下为正
    Alignment X 为0, 按钮在同一水平线画布外; 为1, 在同一水平线画布里


  4. 修改按钮文本


  5. 修改按钮变量名



查看


退出游戏


Quit Game


说明

在主菜单右上角添加按钮, 点击则退出游戏


KismetSystemLibrary

退出游戏

1/** 
2 *	Exit the current game 
3 * @param	SpecificPlayer	The specific player to quit the game. If not specified, player 0 will quit.
4 * @param	QuitPreference	Form of quitting.
5 * @param	bIgnorePlatformRestrictions	Ignores and best-practices based on platform (e.g on some consoles, games should never quit). Non-shipping only
6 */
7UFUNCTION(BlueprintCallable, Category="Game",meta=(WorldContext="WorldContextObject", CallableWithoutWorldContext))
8static void QuitGame(const UObject* WorldContextObject, class APlayerController* SpecificPlayer, TEnumAsByte<EQuitPreference::Type> QuitPreference, bool bIgnorePlatformRestrictions);

QuitPreference

-
Quit 退出
Background 后台运行
 1UENUM()
 2namespace EQuitPreference
 3{
 4    enum Type
 5    {
 6        /** Exit the game completely. */
 7        Quit,
 8        /** Move the application to the background. */
 9        Background,
10    };
11}

在STUMenuWidget实现退出逻辑

  1. 添加属性: 按钮

    protected

    ShootThemUp: Menu/UI/STUMenuWidget.h

    1UPROPERTY(meta = (BindWidget))
    2UButton *QuitGameButton;
  2. 实现回调函数

    private

    ShootThemUp: Menu/UI/STUMenuWidget.h

    1UFUNCTION()
    2void OnQuitGame();

    ShootThemUp: Menu/UI/STUMenuWidget.cpp

    1#include "Kismet/KismetSystemLibrary.h"
    2
    3void USTUMenuWidget::OnQuitGame()
    4{
    5    UKismetSystemLibrary::QuitGame(this, GetOwningPlayer(), EQuitPreference::Quit, true);
    6}
  3. 在NativeOnInitialized注册回调函数

    ShootThemUp: Menu/UI/STUMenuWidget.cpp

    1// NativeOnInitialized
    2
    3if (QuitGameButton)
    4{
    5    QuitGameButton->OnClicked.AddDynamic(this, &USTUMenuWidget::OnQuitGame);
    6}

为WBP_Menu添加退出按钮

  1. 拷贝按钮


  2. 粘贴为画布下级


  3. 设置大小盒

    右上角


    -
    Position X 往右为正
    Position Y 往下为正
    Alignment X 为0, 按钮在同一水平线画布外; 为1, 在同一水平线画布里


  4. 修改按钮文本


  5. 修改按钮变量名



查看