C++ UMG uses Button to control model displacement (timeline) and change materials

UE C++ UMG Using Button

ButtonDemoManager.h

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/TimelineComponent.h"
#include "ButtonDemoManager.generated.h"

UCLASS()
class LEARNCPRO_API AButtonDemoManager : public AActor
{
    
    
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AButtonDemoManager();
	UPROPERTY(EditAnywhere, Category = "UserWidget")
		TSubclassOf<class UMyUserWidgetBtton> WidgetClass;

	UPROPERTY(BlueprintReadWrite,EditAnywhere,Category="Demo")
	AActor* box;
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Demo")
	FVector NewBoxPositionOffset;
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Demo")
	class UMaterial* NewMat;

	FVector InitialLcation;
	FVector EndLocation;
	class UCurveFloat* TimeLineCurve;
	 FTimeline PlatformTimeline;
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION()
		void ChangeBoxPosition();
	UFUNCTION()
		void ChangeBoxMat();
	UFUNCTION()
		void MoveBoxTimeLine(float Value);
};

ButtonDemoManager.cpp

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


#include "ButtonDemoManager.h"
#include "MyUserWidgetBtton.h"
#include "UMG\Public\Components\Button.h"

#include "Kismet/GameplayStatics.h"

// Sets default values
AButtonDemoManager::AButtonDemoManager()
{
    
    
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	static ConstructorHelpers::FObjectFinder<UCurveFloat>Curve(TEXT("/Game/MY/Cure/ButtonCure.ButtonCure"));
	if (Curve.Succeeded())
	{
    
    
		TimeLineCurve = Curve.Object;
	}
}

// Called when the game starts or when spawned
void AButtonDemoManager::BeginPlay()
{
    
    
	Super::BeginPlay();

	APlayerController* myPlayerController = Cast<APlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
	//设置鼠标输入模式
	if (myPlayerController != nullptr)
	{
    
    
		myPlayerController->bShowMouseCursor = true;
		FInputModeGameOnly InputMode;
		InputMode.SetConsumeCaptureMouseDown(true);
		myPlayerController->SetInputMode(InputMode);
	}

	UMyUserWidgetBtton* widget = CreateWidget<UMyUserWidgetBtton>(GetWorld(), WidgetClass);
	if (widget != nullptr)
	{
    
    
		widget->AddToViewport();
	}
	widget->ChangePosition->OnClicked.__Internal_AddDynamic(this, &AButtonDemoManager::ChangeBoxPosition, FName("ChangeBoxPosition"));
	widget->ChangeMat->OnClicked.__Internal_AddDynamic(this, &AButtonDemoManager::ChangeBoxMat, FName("ChangeBoxMat"));

	//TimeLine
	FOnTimelineFloatStatic FirstUpTimeLineCallBack;
	FirstUpTimeLineCallBack.BindUFunction(this, TEXT("MoveBoxTimeLine"));
	PlatformTimeline.AddInterpFloat(TimeLineCurve, FirstUpTimeLineCallBack);

	InitialLcation = box->GetActorLocation();
	EndLocation = InitialLcation + NewBoxPositionOffset;
	PlatformTimeline.SetPlayRate(1);
}

// Called every frame
void AButtonDemoManager::Tick(float DeltaTime)
{
    
    
	Super::Tick(DeltaTime);
	PlatformTimeline.TickTimeline(DeltaTime);
}

//直接移动
//void AButtonDemoManager::ChangeBoxPosition()
//{
    
    
//	box->AddActorLocalOffset(NewBoxPositionOffset);
//}
//TimeLine移动
void AButtonDemoManager::ChangeBoxPosition()
{
    
    
	PlatformTimeline.PlayFromStart();
}

void AButtonDemoManager::ChangeBoxMat()
{
    
    
	UStaticMeshComponent* MeshCom = Cast<UStaticMeshComponent>(box->GetComponentByClass(UStaticMeshComponent::StaticClass()));
	MeshCom->SetMaterial(0, NewMat);
}

void AButtonDemoManager::MoveBoxTimeLine(float Value)
{
    
    
	//GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::SanitizeFloat(Value));
	InitialLcation = FMath::Lerp(InitialLcation, EndLocation, TimeLineCurve->GetFloatValue(Value));
	box->SetActorRelativeLocation(InitialLcation);
}


MyUserWidgetBtton.h

// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "MyUserWidgetBtton.generated.h"
/**
 * 
 */
UCLASS()
class LEARNCPRO_API UMyUserWidgetBtton : public UUserWidget
{
    
    
	GENERATED_BODY()
	
public:
	UPROPERTY(meta = (Bindwidget))
		class UButton* ChangePosition;
	UPROPERTY(meta = (Bindwidget))
		class UButton* ChangeMat;
};

MyUserWidgetBtton.cpp //Write two functions that bind UMG buttons to control model displacement and change state (no time to write)...

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


#include "MyUserWidgetBtton.h"


Guess you like

Origin blog.csdn.net/qq_34708792/article/details/121169933