C++ widget(小白临时记录)

 ue5的userwidge中的构造函数写法

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "MyUserWidget.generated.h"

/**
 * 
 */
UCLASS()
class SAVEDATATOFILE_API UMyUserWidget : public UUserWidget
{
	GENERATED_BODY()

public:
	
    //构造函数
	UMyUserWidget(const FObjectInitializer& ObjectInitializer);
		
	//创建蓝图类,在编辑器中指认蓝图类对象
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets")
	TSoftClassPtr BP_WidgetClass;
	
	UFUNCTION(BlueprintCallable)
	void loaduserwidget();

	void OnButton0Clicked();
};

用路径查找蓝图类进行软引用



#include "MyUserWidget.h"
#include "Blueprint/UserWidget.h"
#include "Components/Button.h"


UMyUserWidget::UMyUserWidget(const FObjectInitializer& ObjectInitializer)
	:Super(ObjectInitializer)
{
	
}
void UMyUserWidget::loaduserwidget()
{
	//通过路径的方式对蓝图类做软引用
    FSoftClassPath softclass(TEXT("WidgetBlueprint'/Game/BP_MyWidget.BP_MyWidget_C'"));
	UClass* widgetclass = softclass.TryLoadClass();
	BP_WidgetClass = widgetclass;
	
	if (BP_WidgetClass.IsValid())
	{
		BP_WidgetClass.LoadSynchronous();

		// 创建正确的用户界面类实例
		UUserWidget* BP_WidgetInstance = CreateWidget(GetWorld(), BP_WidgetClass.Get());
		if (BP_WidgetInstance)
		{
			UButton* buttonBP = Cast(BP_WidgetInstance->GetWidgetFromName(TEXT("Button01")));
			
			buttonBP->OnClicked.AddDynamic(this,&UMyUserWidget::BeButtonClicked);
		}
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, TEXT("Invalid BP_WidgetClass!"));
	}
}

void UMyUserWidget::BeButtonClicked()
{
	GEngine->AddOnScreenDebugMessage(-1,0.5f,FColor::Red,TEXT("this ok"));
}

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "MyUserWidget01.generated.h"

/**
 * 
 */
UCLASS()
class WIDGETTEST001_API UMyUserWidget01 : public UUserWidget
{
	GENERATED_BODY()

protected:
	
	UPROPERTY(BlueprintReadWrite,meta = (bindwidget))
	class UTextBlock* TextLabel;

	UPROPERTY(BlueprintReadWrite,meta = (bindwidget))
	class UButton* testBtn;

	virtual void NativeConstruct() override;

	UFUNCTION()
	void testBtnOnClicked();

	
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyUserWidget01.h"

#include "Components/Button.h"
#include "Components/TextBlock.h"

void UMyUserWidget01::NativeConstruct()
{
	if (testBtn)
	{
		testBtn->OnClicked.AddDynamic(this,&UMyUserWidget01::testBtnOnClicked);
	}
}

void UMyUserWidget01::testBtnOnClicked()
{
	TextLabel->SetText(FText::AsNumber(17));
}
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "widgetTest001GameModeBase.generated.h"

/**
 * 
 */
UCLASS()
class WIDGETTEST001_API AwidgetTest001GameModeBase : public AGameModeBase
{
	GENERATED_BODY()

protected:
	
	UPROPERTY(EditAnywhere,Category = "Class Types")
	//定义一个变量,该变量持有从 UUserWidget 类派生的类的引用,TSubclassOf虚幻中的一个模版类,用于表示一个类的引用。
	TSubclassOf widgetclass;

	UPROPERTY(VisibleInstanceOnly,Category = "Runtime")
	//创建一个MyUserWidget01类型的变量
	class UMyUserWidget01* MyUserWidget01;

	virtual void BeginPlay() override;


};
// Copyright Epic Games, Inc. All Rights Reserved.


#include "widgetTest001GameModeBase.h"
#include "MyUserWidget01.h"

#include "Blueprint/UserWidget.h"

void AwidgetTest001GameModeBase::BeginPlay()
{
	if (IsValid(widgetclass))
	{
		MyUserWidget01 =Cast (CreateWidget(GetWorld(), widgetclass));
		if (MyUserWidget01 != nullptr)
		{
			MyUserWidget01->AddToViewport();
		}

	}
	
}	

ue4创建窗口

int AwidgetTest001GameModeBase::newwindow()
{
	
	
	TSharedPtr mianwin = SNew(SWindow).ClientSize(FVector2D(1280.f, 720.f))[SNew(SImage)];

	FSlateApplication::Get().AddWindow(mianwin.ToSharedRef());
	while (!GIsRequestingExit)
	{
		FSlateApplication::Get().Tick();
		FSlateApplication::Get().PumpMessages();
	}
	FSlateApplication::Shutdown();
	return 0;
	
	
}

你可能感兴趣的:(c++,开发语言)