栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ UMG 使用Button属性1105

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ UMG 使用Button属性1105

C++ UMG 使用Button属性1105

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 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 "UMGPublicComponentsButton.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::FObjectFinderCurve(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(UGameplayStatics::GetPlayerController(GetWorld(), 0));
	//设置鼠标输入模式
	if (myPlayerController != nullptr)
	{
		myPlayerController->bShowMouseCursor = true;
		FInputModeGameonly InputMode;
		InputMode.SetConsumeCaptureMouseDown(true);
		myPlayerController->SetInputMode(InputMode);
	}

	UMyUserWidgetBtton* widget = CreateWidget(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(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 //里面啥都没有…

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


#include "MyUserWidgetBtton.h"


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433333.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号