Unreal Engine C++ 入门01

1.UMG
UMG
向关卡增加UI控件

比如按钮和文本之类

在这里插入图片描述
打开unreal engine
我安装的是在这里插入图片描述
C++开发的话有同时安装vs2019
VS2019的话安装有注意组件问题,不然创建会报错。
windows sdk
net sdk
等有些东西有自已选 择安装

新建项目
在这里插入图片描述
选 择空白模板

在这里插入图片描述

选择C++项目
在这里插入图片描述
资源文件和c++类
在这里插入图片描述
打开C++ 类就可以打vs2019
ps::: 因为我增加过文件了,项目创建时就一个类的,我这边增加了类。
HowTo_UMGGameModeBase
HowTo_UMG是新建项目时,你的项目名字,
GameModeBase是中文 过来的意思是游戏模式基础类
HowTo_UMGPlayerController是增加类

在这里插入图片描述

在这里插入图片描述

第一下,引擎构建器

using UnrealBuildTool;

public class HowTo_UMG : ModuleRules
{
public HowTo_UMG(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

	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
}

}

在这里插入图片描述

在HowTo_UMGGameModeBase.h

增加#include “Blueprint/UserWidget.h”
这个是蓝图的用户界面头文件

1>HowTo_UMGGameModeBase.h

#pragma once
#include “Blueprint/UserWidget.h”
#include “Components/Button.h”
#include “CoreMinimal.h”
#include “GameFramework/GameModeBase.h”
#include “HowTo_UMGGameModeBase.generated.h”

/**
*
*/
UCLASS()
class HOWTO_UMG_API AHowTo_UMGGameModeBase : public AGameModeBase
{
GENERATED_BODY()

public:
/** 移除当前菜单控件,并在指定类(如有)中新建控件。*/
//UFUNCTION
UFUNCTION(BlueprintCallable, Category = “UMG Game”)
void ChangeMenuWidget(TSubclassOf NewWidgetClass);

protected:
/** 游戏开始时调用。 游戏开始 */
virtual void BeginPlay() override;

/** 游戏开始时,用作菜单的控件类。*/
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "UMG Game")
    TSubclassOf<UUserWidget> StartingWidgetClass;

/** 用作菜单的控件实例。*/
UPROPERTY()
    UUserWidget* CurrentWidget;

};

引擎用了很多跟java 差不多的东西反射和注解

UCLASS() 引擎的类的定义

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = “UMG Game”)
向蓝图提供只读功能,也就是说,这个新建蓝图之后可以防问这个定义的内容

2>HowTo_UMGGameModeBase.cpp

#include “HowTo_UMGGameModeBase.h”

void AHowTo_UMGGameModeBase::BeginPlay()
{
Super::BeginPlay();

ChangeMenuWidget(StartingWidgetClass);

}

void AHowTo_UMGGameModeBase::ChangeMenuWidget(TSubclassOf NewWidgetClass)
{
if (CurrentWidget != nullptr)
{
CurrentWidget->RemoveFromViewport();
CurrentWidget = nullptr;
}
if (NewWidgetClass != nullptr)
{
CurrentWidget = CreateWidget(GetWorld(), NewWidgetClass);
if (CurrentWidget != nullptr)
{
CurrentWidget->AddToViewport();
}
}
}

增加HowTo_UMGPlayerController
向VS2019增加新类有引擎编辑器里增加

一定要C++ 类里边,就源码的根目录里边
在这里插入图片描述

新建 C++ 类
在这里插入图片描述
选择玩家控制的类

在这里插入图片描述
h文 件内容
#include "HowTo_UMG.h"放第二个位置
编译时,有时不是代码问题,有清理一下解决方案和清理试一下,

在这里插入图片描述

》》》》》》》
#include “HowTo_UMGPlayerController.h”
#include “HowTo_UMG.h”

void AHowTo_UMGPlayerController::BeginPlay()
{
Super::BeginPlay();
SetInputMode(FInputModeGameAndUI());
}

cpp文 件内容

#pragma once

#include “CoreMinimal.h”
#include “GameFramework/PlayerController.h”
#include “HowTo_UMGPlayerController.generated.h”

/**
*
*/
UCLASS()
class HOWTO_UMG_API AHowTo_UMGPlayerController : public APlayerController
{
GENERATED_BODY()

public:
virtual void BeginPlay() override;

};

以上是VS2019 的操作

在引擎编辑器:

还有创建蓝图,操作 很步骤很多

在这里插入图片描述

换成内容里边

在这里插入图片描述

三个图标是我增加过的了

控件蓝图

在这里边是控件
在这里插入图片描述

蓝图类
这两个类是C++ 一起的

在这里插入图片描述

操作方式
增加控件蓝图
在这里插入图片描述
向界面增加控件

在这里插入图片描述
在这里插入图片描述
控制面板有控件

细节有控件的属性,

在这里插入图片描述
在细节最下面有事件

给按钮增加事件

在这里插入图片描述
事件蓝图

在这里插入图片描述
右点击

在这里插入图片描述

退出游戏节点,
输入Q就可以找到然后连一下连

在这里插入图片描述

下面的按钮点击事件,
节点增加

在这里插入图片描述
这个节点是输入类名前这几个字母就能找

在这里插入图片描述
得到游戏模式

在这里插入图片描述

change menu widget 的节点增加方法,
先择在这里插入图片描述
一边拖,然后有输入ch 就能把新节点查找了,

在这里插入图片描述
C++和蓝图交互的方式
在这里插入图片描述
蓝图完成之后

增加蓝图类

在这里插入图片描述

增加分别增加这两个的蓝图类

在这里插入图片描述
MenuGameMode 类增加 那个新创建 的控件蓝图
在这里插入图片描述

在这里插入图片描述

在关卡设置 把MenuGameMode 增加进就可以运行看到gui 了
在这里插入图片描述

在这里插入图片描述

运行之后的结果,
点击有退出游戏 事件的按钮就会退出游戏

在这里插入图片描述

这一个流程下来,总之流程很麻烦,

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unreal Engine 4 Scripting with C++ Cookbook 2016 | ISBN-10: 1785885545 | 431 pages | PDF | 7 MB Key Features A straightforward and easy-to-follow format A selection of the most important tasks and problems Carefully organized instructions to solve problems efficiently Clear explanations of what you did Solutions that can be applied to solve real-world problems Book Description Unreal Engine 4 (UE4) is a complete suite of game development tools made by game developers, for game developers. With more than 100 practical recipes, this book is a guide showcasing techniques to use the power of C++ scripting while developing games with UE4. It will start with adding and editing C++ classes from within the Unreal Editor. It will delve into one of Unreal's primary strengths, the ability for designers to customize programmer-developed actors and components. It will help you understand the benefits of when and how to use C++ as the scripting tool. With a blend of task-oriented recipes, this book will provide actionable information about scripting games with UE4, and manipulating the game and the development environment using C++. Towards the end of the book, you will be empowered to become a top-notch developer with Unreal Engine 4 using C++ as the scripting language. What you will learn Build function libraries (Blueprints) containing reusable code to reduce upkeep Move low-level functions from Blueprint into C++ to improve performance Abstract away complex implementation details to simplify designer workflows Incorporate existing libraries into your game to add extra functionality such as hardware integration Implement AI tasks and behaviors in Blueprints and C++ Generate data to control the appearance and content of UI elements
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值