zoukankan      html  css  js  c++  java
  • UE4使用UMG接口操作界面

    原文链接:http://gad.qq.com/article/detail/7181131

    本文首发腾讯GAD开发者平台,未经允许,不得转载


    UE4的蓝图之强大让人欲罢不能,但是实际在项目的开发中,C++与蓝图都需要结合使用,单独选择一样开发都不是特别科学,这里我就来研究了一下C++使用UMG接口来操作界面,我的目的非常简单,用C++来创建界面,并在创建成功的时候,告诉界面打印出相关信息。

    1.创建一个C++的空模板工程,命名UMGProject,用VS打开工程文件,找到UMGProject.Build.cs,在PublicDependencyModuleNames后面添加UMG模块,并取消PrivateDependencyModuleNames的注释,并编译工程。
    using UnrealBuildTool;
    public class UMGProject : ModuleRules
    {
    	public UMGProject(TargetInfo Target)
    	{
    		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore","UMG" });
    
    		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
    	}
    }

    2.回到UE4的工程中,在Content下创建一个Blueprint的文件夹,在Blueprint下右键空白处点击选择User Interface/Wiget Blueprint,命名BP_UI
    3.双击打开BP_UI,在中间添加一个Text控件,并设置为显眼一点的红色,并设置它的IsVariable为true,后面再创建函数的时候才方便使用。
    4.并且我在BP_UI里面创建一个函数接口,在右上角点击Graph,添加一个Functions,命令为ShowMessage,功能就是用Text显示传送过来的信息。

    5.在C++ Classes下面创建一个新的Class,继承自PlayerController,命名为UMGPlayerController,用VS打开后,我们需要在UMGPlayerController.h文件中的UCLASS里面添加Abstract和Blueprintable,UCLASS表示类修饰符,里面的参数是类具体的表述。
    Abstract 类修饰符将类声明为“抽象基类”,这样会阻止用户在虚幻编辑器中向这个世界中添加这个类的Actor,或者在游戏过程中创建这个类的实例,而Blueprintable指定该类为创建蓝图的可接受基类。除非被继承,否则默认值为NotBlueprintable。它由子类继承。
    同时在这个类中,我们还会用到一个函数修饰符,BlueprintImplementableEvent,表示此函数可以在蓝图或关卡蓝图图表内进行重载。这里就是用于C++与蓝图的交互接口,同时这个函数不用在cpp中再去实现它,完成后编译。
    #include "GameFramework/PlayerController.h"
    #include "UMGPlayerController.generated.h"
    /**
     * 
     */
    UCLASS(Abstract, Blueprintable)
    class UMGPROJECT_API AUMGPlayerController : public APlayerController
    {
    	GENERATED_BODY()
    public:
    		/**此函数可以在蓝图或关卡蓝图图表内进行重载*/
    		UFUNCTION(BlueprintImplementableEvent, Category = "UMG")
    		void PrintMessage(const FString &Message);
    };

    6.在ue4中,Content/Blueprint下创建一个新的蓝图继承自上面所写的UMGPlayerController,命名为BP_UMGController。
    7.我们再打开C++下面的UMGProjectGameMode,详细解释,都写在了注释中,主要在C++的顶上要添加UMG.h的头文件,在UMG.h包括了UMG整个模块的所有头文件,所以我们只添加它一个就可以了,在完成后,编译一下。
    头文件:
    #pragma once
    #include "UMG.h"
    #include "GameFramework/GameMode.h"
    #include "UMGProjectGameMode.generated.h"
    /**
     * 
     */
    UCLASS()
    class UMGPROJECT_API AUMGProjectGameMode : public AGameMode
    {
    	GENERATED_BODY()
    public:
    	AUMGProjectGameMode();
    	~AUMGProjectGameMode();
    
    	virtual void BeginPlay() override;
    	//获取菜单
    	UFUNCTION(BlueprintCallable, Category = "UMG")
    	UUserWidget* GetCountWidget();
    
    protected:
    	//菜单
    	UPROPERTY()
    	UUserWidget* CountWidget;
    	//创建菜单
    	void CreateCountWidget();
    };

    源文件:

    #include "UMGProject.h"
    #include "UMGProjectGameMode.h"
    #include "UMGPlayerController.h"
    
    AUMGProjectGameMode::AUMGProjectGameMode()
    {
    	//通过路径找到蓝图,并将蓝图控制器的类设置给PlayerControllerClass  
    	static ConstructorHelpers::FClassFinder<AUMGPlayerController> UMGControllerClassFinder(TEXT("/Game/Blueprint/BP_UMGController"));
    	PlayerControllerClass = UMGControllerClassFinder.Class;
    
    	CreateCountWidget();
    }
    AUMGProjectGameMode::~AUMGProjectGameMode()
    {
    }
    
    void AUMGProjectGameMode::BeginPlay()
    {
    	Super::BeginPlay();
    	//获取PlayerController并调用开始所写的PrintMessage函数
    	Cast<AUMGPlayerController>(GetWorld()->GetFirstPlayerController())->PrintMessage(TEXT("开始游戏~~~"));
    }
    
    UUserWidget* AUMGProjectGameMode::GetCountWidget()
    {
    	//返回计数界面
    	return CountWidget;
    }
    
    void AUMGProjectGameMode::CreateCountWidget()
    {
    	//通过路径找到蓝图
    	static ConstructorHelpers::FClassFinder<UUserWidget> UMGClassFinder(TEXT("/Game/Blueprint/BP_UI"));
    	//通过蓝图的类,创建菜单
    	CountWidget = CreateWidget<UUserWidget>(GetWorld(), UMGClassFinder.Class);
    	//添加到视图中
    	if (CountWidget != nullptr)
    		CountWidget->AddToViewport();
    }

    8.再打开ue4的BP_UMGController,点击Functions的Override,就会看到我们所写的PrintMessage
    然后点击PrintMessage链接以下蓝图,就是在调用PrintMessage的时候,我们再去调用UI的ShowMessage,将传来的Message显示出来。
    9.编译运行,运行顺序差不多就是在GameMode的构造函数里面设置PlayerController,然后通过路径找到并创建我们的BP_UI,然后再在BeginPlay里面输出文字信息。
    感觉绕了一大圈,做了一个特别小的功能,不过这里主要也是为了表现C++使用UMG的接口,和C++调用蓝图的函数,在实际开发中C++与蓝图的交互是相当重要的。

  • 相关阅读:
    0314:翻车总结
    机器人系统仿真(十一)——URDF集成Gazebo
    机器人系统仿真(十)——arbotix控制机器人运动
    机器人系统仿真(九)——xacro+摄像头+雷达传感器
    机器人系统仿真(八)——xacro小车底盘模型案例
    机器人系统仿真(七)——xacro语法详解
    机器人系统仿真(六)——xacro基本语法
    机器人系统仿真(五)——URDF工具
    机器人系统仿真(四)——四轮圆柱状机器人模型
    C++去除字符串空格和tab
  • 原文地址:https://www.cnblogs.com/liang123/p/6325837.html
Copyright © 2011-2022 走看看