Unreal Engine 4 C++ 动态加载UMG界面

本文介绍如何使用C++在Unreal Engine 4中动态加载UMG(User Interface in the Game)界面。主要内容包括:添加必要的UMG依赖、创建蓝图控件界面、编写用于加载和显示界面的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unreal Engine 4 C++ 动态加载UMG界面

最近研究了一下Unreal Engine4,本人不太习惯蓝图打开界面,就研究了下用C++来打开、显示界面。
好记性不如烂笔头啊,还是记录一下!

1.添加UMG依赖

在你的工程下找到以下文件 :

  • YourProject.Build.cs

添加如下代码:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "Slate", "SlateCore" });

添加后的完整文件:

  • YourProject.Build.cs
// Fill out your copyright notice in the Description page of Project Settings.

using UnrealBuildTool;

public class YourProject : ModuleRules
{
    public YourProject(TargetInfo Target)
    {
        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "Slate", "SlateCore" });

        PrivateDependencyModuleNames.AddRange(new string[] {  });

        // Uncomment if you are using Slate UI
        // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

        // Uncomment if you are using online features
        // PrivateDependencyModuleNames.Add("OnlineSubsystem");

        // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
    }
}

在你的工程下找到以下文件 :

  • YourProject.h

添加如下代码:

#include "Runtime/UMG/Public/UMG.h"
#include "Runtime/UMG/Public/UMGStyle.h"
#include "Runtime/UMG/Public/Slate/SObjectWidget.h"
#include "Runtime/UMG/Public/IUMGModule.h"
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"

添加后的完整文件:

  • YourProject.Build.cs
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Engine.h"

#include "Runtime/UMG/Public/UMG.h"
#include "Runtime/UMG/Public/UMGStyle.h"
#include "Runtime/UMG/Public/Slate/SObjectWidget.h"
#include "Runtime/UMG/Public/IUMGModule.h"
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"

2.创建一个界面

创建一个蓝图控件,取名为Test,如图:

创建蓝图控件

拖拽几个测试控件,如图:

编辑器界面


3.添加代码

  • YourGameMode.h
#pragma once

#include "GameFramework/GameMode.h"
#include "Blueprint/UserWidget.h"
#include "YourGameMode.generated.h"

/**
 * 
 */
UCLASS()
class YOURGAMEMODE_API AYourGameMode : public AGameMode
{
    GENERATED_BODY()

public:
    // 开始游戏
    virtual void StartPlay() override;

private:

    /** 在游戏开始时我们将作为菜单使用的控件类。 */
    UPROPERTY(EditAnywhere)
    TSubclassOf<UUserWidget> StartingWidgetClass;

    /** 用作为界面的实例。 */
    UPROPERTY(EditAnywhere)
    UUserWidget* CurrentWidget; 
};
  • YourGameMode.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "YourGameProject.h"
#include "YourGameModeGameMode.h"

// 开始游戏
void AYourGameMode::StartPlay()
{
    Super::StartPlay();

    /** 游戏开始后弹出界面 */
    StartingWidgetClass = LoadClass<UUserWidget>(NULL, TEXT("Blueprint'/Game/UI/Test.Test_C'"));
    if (StartingWidgetClass != nullptr) 
    {
        CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), StartingWidgetClass);
        if (CurrentWidget != nullptr)
        {
            CurrentWidget->AddToViewport();
        }
    }
}

注意:这里有个大坑,动态加载UClass时需要在代码结尾增加“_C”

StartingWidgetClass = LoadClass<UUserWidget>(NULL, TEXT("Blueprint'/Game/UI/Test.Test_C'"));

4.编译测试

编译后可直接运行,如果成功显示出界面:

这里写图片描述

成功加载界面。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值