创建c++项目,项目build.cs中开启模块:
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
public class OpencvT : ModuleRules
{
public OpencvT(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[]
{
"Core",
"CoreUObject",
"Engine",
"InputCore",
"EnhancedInput",
"OpenCV",
"OpenCVHelper",
"UMG",
"Slate",
"SlateCore",
"ImageWrapper"
});
PrivateDependencyModuleNames.AddRange(new string[] { });
}
}
编译器中创建userwidget的c++类,BP_userwidget的蓝图用户界面,BP_userwidget中添加image小部件,在userwidget.h中添加绑定image,userwidget.cpp中不用添加其他代码。
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Components/Image.h"
#include "MyUserWidget.generated.h"
/**
*
*/
UCLASS()
class OPENCVT_API UMyUserWidget : public UUserWidget
{
GENERATED_BODY()
UMyUserWidget(const FObjectInitializer& ObjectInitializer);
virtual void NativeConstruct() override;
public:
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UImage* cv_Image;
};
创建actor c++类,添加头文件,创建userwidget实例,用来设置绑定的image,创建bp_用户界面实例,添加到视口,将actor类放入场景中,运行。
需要注意:cv加载数据为空或不正确时会引起崩溃
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyOpenCVClass.generated.h"
UCLASS()
class OPENCVT_API AMyOpenCVClass : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyOpenCVClass();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditAnywhere)
UMaterialInterface* BaseMaterial;
UPROPERTY(BlueprintReadOnly,VisibleAnywhere)
UTexture2D* test2d;
//这里需要在编辑器内将bp_用户界面添加到NewWidgetBlueprint
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UI")
TSubclassOf<UUserWidget> NewWidgetBlueprint;
};